PHP Speed: Private vs Public Member And Static Methods
I did some speed testing for my PHP framework(still alpha) and I discovered some surprising results.
For the test I have PHP 5.2.3 and MySQL 5.0.41 with 100000 records varchar(250).Tested with Xdebug.
I wrote 3 classes one with Public Members(Public Methods) , Private Members(Public Methods) and Public Static Members(Public Static Members) with same structure.
I tested the time and ram usage:
| Time(sec) | RAM(Mb) | |
| Private | 1,53 | 36 |
| Public | 1,27 | 36 |
| Static | 1,47 | 0,15 |
*Lower is better.
For just declaring Public over Private Members I gain 20% of speed and RAM consumption remains the same(both methods are public).
And the thing that surprised me the most is RAM consumption of static members with static methods just 0,15 MB!! Wow! Then I tested from other online libraries with private or public(methods and members) and the RAM consummation was always very high.
Do you have similar experience?
- Login or register to post comments
- 3913 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
Michael Kimsal replied on Wed, 2009/01/28 - 9:12am
Vladimir Carrer replied on Wed, 2009/01/28 - 10:07am
ellisgl replied on Wed, 2009/01/28 - 11:54am
Michael Kimsal replied on Wed, 2009/01/28 - 1:03pm
in response to: ellisgl
ellisgl replied on Wed, 2009/01/28 - 1:49pm
in response to: mgkimsal
If their app is using 4GB, then they probably have other issues to deal with.
Michael Kimsal replied on Wed, 2009/01/28 - 2:04pm
nspirit replied on Wed, 2009/01/28 - 2:20pm
Interesting test between private/public speed, but not really surprising for memory
Can you provide the source for the test cases to reproduce similar result ?
Am interested in testing your test case on my php version :)
ellisgl replied on Wed, 2009/01/28 - 2:45pm
in response to: mgkimsal
Vladimir Carrer replied on Wed, 2009/01/28 - 3:37pm
nspirit replied on Thu, 2009/01/29 - 10:13am
Actually it wasn't about your framework, and i really hope to see a good product that'll meet my own needs :).
It was more about "How did you produce this result ?". And more than that, in which case did you use a static ?
Because between 36 and 0.15 there's a big gap. To be like that it must be a shared data between every single instance of class. Because if use to store data specific to each object, it would be inapropriate.
As you prefer to not show some small part of you code (you must have your reasons) i'll wait the public release. Keep us informed :)
Philippe Lhoste replied on Tue, 2009/04/07 - 6:14am
As said, it would be interesting to see how you did the test, it is quite meaningless without context.
Indeed, the public vs. private result is surprising, but the presence of static in the mix is odd. I will suppose you know what static is, so, since data there is shared between all instances of the classes you made (how many instances?), it expected to consume far less memory. Except that usage is very different of non-static variables, so I don't know why you compare them.