[Phpug] Performance: Static versus Intance Methods

Pádraic Brady padraic.brady at yahoo.com
Wed Sep 12 10:23:12 IST 2007


Michał is dead on.

Static methods, in general, are somewhat faster. But if you were to take your application, setup Derrick Rethan's xdebug extension, and analyse the cachegrind file it generates for performance data you might be surprised. Objects in PHP perform well - even better since PHP 5.2. Using the same object multiple times compared to using the same static method multiple times might not raise much of a measurable difference.

It probably would not be a bad idea to invest some time in using xdebug. It's incredibly useful for analysing PHP performance and figuring out the hotspots where your performance optimising time is best spent.

Paddy


 
Pádraic Brady
http://blog.astrumfutura.com
http://www.patternsforphp.com


----- Original Message ----
From: "Słaby, Michał" <mslaby at paddypower.com>
To: Irish PHP User Group <phpug at lists.iephpug.org>
Sent: Wednesday, September 12, 2007 9:51:00 AM
Subject: Re: [Phpug] Performance: Static versus Intance Methods

On Tue, 2007-09-11 at 10:57 +0100, David Foley wrote:
> In order to reduce the 'weight' of instances at runtime, I've modified those
> methods
> to simply invoke static functions implemented in a seperate Characters
> class, the idea being that the move will have a positive impact
> on performance overall in cases where lots of String instances are being
> created and operated on - the idea (assumed principle?) being
> that many 'light' object instances utilising one 'heavy' objects methods is
> preferable to lots of 'heavy' object instances residing
> in memory..

Hi David,

I used to think about object the same way. I thought they are 'heavy' in
terms of memory consumption and performance. Of course I couldn't and
still can't prove the heaviness. In fact, objects in PHP are blazing
fast and fairy lightweight. Objects are so fast, that Zend Engine
developers advise to wrap arrays in objects to gain speed:

  foreach (new ArrayObject(array(1, 2, 3)) as $i)

is considerably faster than

  foreach (array(1, 2, 3) as $i)

Weight of instance is nothing more than sum of its properties (not
methods). Memory for storing methods is allocated per class, not per
instance.

The only performance drawback in objects is that working on properties
is ~ 2 times slower than working on local variables (there was cool
performance loss table in polish edition of PHP Solutions a year ago,
btw. I was shocked to see that using uninitialized variable can be up to
9 times slower than initialized one!).

> So - in general which is better - simply implementing everything in String
> (in this case), or using a 'companion' class (like
> Characters in this case) to hold all the 'weight' of possibly intensive
> functions, or refactoring instance methods in terms of method calls to
> static methods
> of another 'companion' class? After all, static methods don't require any
> objects to be instantiated, and static methods can be used by other
> classes without locking one into instantiation procedures. .It should be
> less memory intensive, but knowing that is out of my leagu.

What I learned over years is that computer time is cheap and developer
time is *very* expensive. So, my advice would be: do it the way it is
the most convenient for you to use and don't care too much about
performance. Some optimization can be done afterwards.


Good luck,
Michael

________________________________________________________________________
Privileged, confidential and/or copyright information may be contained in this communication. This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended addressee, you may not copy, forward, disclose or otherwise use this e-mail or any part of it in any way whatsoever. To do so is prohibited and may be unlawful. If you have received this email in error 
please notify the sender immediately.

Paddy Power PLC may monitor the content of e-mail sent and received for the purpose of ensuring compliance with its policies and procedures.

Paddy Power plc, Airton House, Airton Road, Tallaght, Dublin 24  Registered in Ireland: 16956
________________________________________________________________________
_______________________________________________
Phpug mailing list
Phpug at lists.iephpug.org
http://lists.iephpug.org/mailman/listinfo/phpug
Member Map http://www.frappr.com/irishphpusersgroup/
Membership Planner Calendar: http://tinyurl.com/opjd5







      ____________________________________________________________________________________
Shape Yahoo! in your own image.  Join our Network Research Panel today!   http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 



More information about the Phpug mailing list