and3k Posted May 30, 2007 Share Posted May 30, 2007 Hi there, since PHP5 is out, i'm using oop very extensivly, so in some of my bigger projects i have more than 50 classes and i'm loading over 200 instances of them every pageview, so here my question: Is this making PHP very slow? For example, what is faster: <?php $instance=new DataManipulation("Data"); $instance->ManipulateValue(); $instance->echoValue(); ?> or <?php $value="Data"; $value=ManipulateValue($wert); echo $value; ?> As I understand it, in C++ making an instance of a class is nearly as fast as assigning a value to a variable? Is PHP as efficent als C++ oder not? greets, and3k Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/ Share on other sites More sharing options...
Jenk Posted May 30, 2007 Share Posted May 30, 2007 What on gods green Earth requires "more than 50 classes" and "over 200 instances of them" for every page view? Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-264876 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 I know that in PHP 4 having too many classes dramatically increases server load and page load time. I was only using about 3 classes but I was also using classes within classes. But try it out and see how it runs, if it runs good than there is no problem right? Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-264885 Share on other sites More sharing options...
redbullmarky Posted May 30, 2007 Share Posted May 30, 2007 ....if it runs good than there is no problem right? hmmm kinda - but I have to go with Jenk on this one. maybe it's actually the code that's the problem, not PHP. Even in some of the hugest webapps i've dealt with, the figures arent even close - both 50 classes and 200 instances are ridiculously high. Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-264890 Share on other sites More sharing options...
ShogunWarrior Posted May 30, 2007 Share Posted May 30, 2007 I really don't know too much about the object internals of PHP but the main thing I'd be thinking about is the disk-access time to read the number of files you have those classes in and the compile-and-interpret time. I know in C++ that object overhead is low because class functions only take up one block of space and subsequent instances only take up the space to store their properties but as for PHP I'm not sure but realistically if you can cut the overhead then do. Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-264957 Share on other sites More sharing options...
and3k Posted May 30, 2007 Author Share Posted May 30, 2007 yeah i know thats a lot. till now i had no problems, but none of my projects have really high pageviews (not yet anyway). i wouldn't attempt to do this in php4, according to the php hp, php5 has highly increased speed due to the new zend core (or something like that) aren't there any benchmarks? Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-265196 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 Run a loop 5,000; 10,000 and 50,000 times to "simulate" page views. Using www.php.net/microtime you can tell how long each one took, the average and what the total time of the script was. IE: <?php $time_start = microtime(true); set_time_limit(900); // give our selves ample amount of time before a timeout. for ($i=1;$i<5000; $i++) { $times[$i]['start'] = microtime(true); $instance=new DataManipulation("Data"); $instance->ManipulateValue(); $instance->echoValue(); $instance = null; $times[$i]['end'] = microtime(true); } $time_end = microtime(true); ?> Unsure if that is a valid test, but yea. You may need to look into the www.php.net/set_time_limit function too Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-265203 Share on other sites More sharing options...
ShogunWarrior Posted May 30, 2007 Share Posted May 30, 2007 frost is right. When trying to benchmark code for one pageview the difference might be barely a few miliseconds but by comparing the time for 1000 loops or a large number like that you get to see how much time you are losing along the way. (Also, and I'll try and find them, there are functions for checking Memory usage in PHP which would be good for profiling your code aswell to check overhead) Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-265232 Share on other sites More sharing options...
btherl Posted June 1, 2007 Share Posted June 1, 2007 OTOH, it is page view times that are the bottom line. A loop won't take into account parsing time, which for 50 classes may be quite significant. The loop is great for optimizing that particular code, but if that code accounts for only a small portion of total execution time then you haven't achieved much. Recently I halved the cpu usage of one of my scripts by culling dead code that was being parsed but not used. That's a 50% improvement just from reducing parsing time. Admittedly this script is quite light on processing, but still.. If parsing time is significant, you may see benefits by loading only those classes necessary for your code path. That can be messy to work out though.. Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-266064 Share on other sites More sharing options...
and3k Posted June 1, 2007 Author Share Posted June 1, 2007 i'm loading every class via the __autoload()-feature of php, but sometimes i need a lot Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-266079 Share on other sites More sharing options...
Jenk Posted June 1, 2007 Share Posted June 1, 2007 You are all missing the point entirely. 200 objects is far too much. Redesign your application. Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-266141 Share on other sites More sharing options...
ShogunWarrior Posted June 1, 2007 Share Posted June 1, 2007 OTOH, it is page view times that are the bottom line. A loop won't take into account parsing time, which for 50 classes may be quite significant. The loop is great for optimizing that particular code, but if that code accounts for only a small portion of total execution time then you haven't achieved much. Recently I halved the cpu usage of one of my scripts by culling dead code that was being parsed but not used. That's a 50% improvement just from reducing parsing time. Admittedly this script is quite light on processing, but still.. If parsing time is significant, you may see benefits by loading only those classes necessary for your code path. That can be messy to work out though.. Good point there. Putting the whole "application" into a loop (i.e including files etc) is much more representative of load time. Quote Link to comment https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/#findComment-266173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.