NYCPHP Meetup

NYPHP.org

[nycphp-talk] new vs clone Performance

Hans Zaunere lists at zaunere.com
Tue Oct 7 12:53:49 EDT 2008


> > Out of curiosity, I wanted to get some benchmarks on using the new operator
> > vs cloning.  I wrote a simple script, attached, which is fun to play with.
> > Upshot:  clone is faster if used correctly (and depending on the object's
> > operations).  Also some interesting observations in the script itself, and
> > I'm interested in hearing other's observations/experiences.
> >
> 
> By faster, how much faster do you mean?

Run the script :)

It depends a lot on the class (and the use of __clone() and the heaviness of the class initialization in __construct()) but I was seeing consistent 20% - 30% across various classes.  It's fun to play around with, using various builtin classes, and user defined ones.  Sometimes, though, the increase was dramatic (use stdClass for example).

> I never really thought of doing this kind of optimization, but it
> would be pretty easy to re-pattern object creation _if it was
> significantly faster_. The script ran out of memory on my Mac, what
> kind of speedup are you seeing, Hans?

Put $OCount to something smaller, like 100,000 or 10,000

Significantly faster will be a hard thing to quantify.  Doing $OCount runs of 1,000,000 typically yielded only a fraction of seconds improvements (and that's when creating 1 million objects which no application really does).  So I'm not sure how much of a real improvement this would have for a typical application.

That said, the reason I started investigating this was because of APC.  Theory being, create objects upon first request when the server comes online, cache them in APC, and then clone them as needed during subsequent requests.  Depending on the object's initialization pattern, this could yield some real per-request performance gains.

Of course, investigating APC's performance is TBD.  Long story short, APC's dopy serialization/un-serialization of variables may make this all moot.

---
Hans Zaunere / Managing Member / New York PHP
      www.nyphp.org  /  www.nyphp.com 






More information about the talk mailing list