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?

Article Type: 
Opinion/Editorial
0

(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

Interesting, but not surprising.  Dare I say this is one more reason not to bother with PPP visibility for most developers in most projects?  If you're writing code with the aim of sharing it with the broader world, PPP can serve a purpose.  If you're writing for yourself or a small team and the code won't go much further, document your recommended class usage, but don't focusing on using PPP the "right" way (cause there isn't one).  Now we can say that using private (did you try protected as well?) will slow down your app too.  I'm guessing code caching would probably reduce the speed issue though.  :)

Vladimir Carrer replied on Wed, 2009/01/28 - 10:07am

@Michael: The project will be for the public in the end, that is why I did the testing. Yes I tried with protected as well and it show similar results like private members.

ellisgl replied on Wed, 2009/01/28 - 11:54am

I can only imagine people going "Well I can't afford ram - i'll make everything static".. *Cringes*..

Michael Kimsal replied on Wed, 2009/01/28 - 1:03pm in response to: ellisgl

can you imagine people with large apps that are already at 4 gig on the machine and they can't really expand that without upgrading operating systems or hardware?  I think the issue here was public vs private primarily, no?

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

A system can use 4gb without it all being one app :)

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

That's true.. But if it where one instance of one script....

Vladimir Carrer replied on Wed, 2009/01/28 - 3:37pm

@nspirit, @ellisg: Thank for the interest, but I don't think is fer to realise my code before the publication. Hopefully at the and of February will be all public  :). You don't need my code to start just take any open php class, phpclasses.org is good place to start and test, test. In my opinion l think we know very little about static methods and is not very clear when or why should we use them.

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.