Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/53590-how-fast-is-oop-in-php/
Share on other sites

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?

....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.

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.

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?

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

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)

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..

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.