[Phpug] Performance: Static versus Intance Methods
David Foley
davi.fol at gmail.com
Tue Sep 11 10:57:47 IST 2007
Hi all - just joined the group today and as it happens, I have a situation I
wouldn't mind some feedback on.
Ok -I've been developing a PHP String class - just a wrapper for php
strings. It lets me do things like this:
// some priori code
$pdfFileName = new String ($item['name']);
$pdfFileName = $pdfFileName ->toHTMLEntities("UTF-8")->replace(".pdf",
"")->replace(".", " ");
rather than this
// some prior code
$pdfFileName = $item['name'];
$pdfFileName = htmlentities($pdfFileName, ENT_COMPAT, "UTF-8");
$pdfFileName = str_replace(".pdf", "", $pdfFileName);
$pdfFileName = str_replace(".", " ", $pdfFileName);
Its not the most dramatic or sensible demonstration in the world, but thats
not the point: the point is that the String class
I'm developing has about 18 methods in all, and Im wondering about
performance impact.in cases where lots of String
instances are being used.
I originally implemented the class entirely in terms of instance methods,
but now I'm recosidering.
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..
(BTW, the reason I simply didn't create private or public static methods on
String, was that I wanted to keep
a method naming convention intact, and method names must be unique to
classes, regardless of whether they are implemented
as instance or class methods).
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.
phpfi was down when I last checked, so I wasn't able to post any code,
though I can do if someone can suggest a way.
Any feedback, good or bad, welcome!
Davi.
More information about the Phpug
mailing list