Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« The Endpoints of the Scale of Stupidity on Video
Sarah Snow Stever »

Benchmarking PHP’s Magic Methods

November 4th, 2007

Larry Garfield has an interesting set of benchmarks covering many of PHP’s magic methods. His results correspond pretty well to my own benchmarks in the area. The thing to take away is that its not necessarily the overhead of the magic methods, but rather what you do inside them. Its hard to do anything useful inside a magic method, such as __get or __call that isn’t 10 to 20 times slower than the “non-magic” solution.

There are probably more than a few naive programmers who would read results like this and start to avoid these constructs in their own code for performance reasons. These are the same kinds of people obsessing over a few single or double quotes, who eschew “slow” objects in favor of switch statements that are many times slower than the polymorphic method calls they are trying to avoid.

But, that’s not the end of the story. Larry ran his benchmarks using 2,000,000 iterations. The N really matters here. Sure, iterators are slower than arrays, but you aren’t going to be iterating over two million things. I tend to fetch my database records in lots of 25 or 50. You aren’t going to be making two million invocations of __call. But how many will you make? Under what value of N does the performance of these techniques cease to matter? Is it ten, one hundred, one thousand, or ten thousand? You may be surprised at how few calls your program actually does and how little impact it has on performance.

As Wez and Travis point out in their comments, profiling is the way to find out the potential impact and to discover your true N.

Paul M. Jones has a good example of what I’m talking about. There, call_user_func_array appears to be a bottleneck, but it turns out that its the function being called, htmlspecialchars, not the calling process that consumes the balance of the time. In that case, the function was “only” called 300 times. I find that order of magnitude to be fairly typical. Something to be aware of, perhaps, but not something to obsess over.

categories PHP, Software Design
tags benchmarks, performance-optimization, profiling

Related Posts

  • Let Your Properties be Properties
  • PDO Design Evolution
  • Agile Development for an Agile Military
  • Code Coverage, Feedback and Open Source
  • PHP Games
You can leave a response, or trackback from your own site.

9 Responses to “Benchmarking PHP’s Magic Methods”

  1. #1 niko responds...
    November 4th, 2007 at 11:30 pm

    Thanks for this post.
    It’s really stupid when people want to speed up their php code this way.

  2. #2 Paul M. Jones responds...
    November 5th, 2007 at 8:02 am

    Hi Jeff — I agree with your points, and want to add a little clarification about my blog post.

    Although using call_user_func_array() is technically about half as fast as a direct function call, you have to remember (1) it’s only one call in a , and you’re not using it very often at that, and (2) in general terms it’s still *very* fast, especially in comparison to all the other things you have to do (e.g. as you point out, hitting the database).

    In short, if your application bottlenecks really are call_user_func_array() or related, and **nothing else**, then you’re doing pretty darn good. :-)

  3. #3 Matt Farina responds...
    November 5th, 2007 at 8:10 am

    I how you use these really depends on what you are doing. Larry is looking at drupal specific uses. And, when something gets implemented in drupal performance is always a big concern. With the number of high volume sites running drupal this makes sense.

    If I have a low volume site performance might not make as much of a difference. If I have a high volume site I’m going to fight for every cpu cycle.

  4. #4 Ve Bailovity responds...
    November 12th, 2007 at 2:47 am

    …
    profiling is the way to find out the potential impact and to discover your true N.
    …

    I totally agree. You can’t really predict where the main bottleneck is going to be, although you can have an educated guess - for instance, polling a database within a loop is most certainly going to be a kludge and you might want to rethink your approach.

    However, optimizing too early can easily leave you with unnecessary “speed hacks” in your code, making it harder to understand and maintain. Perhaps the “right” approach might be pondering about and selecting the optimal algorithm for the task at hand, then implementing it in the most elegant, readable and maintainable way - and then, as you watch your app perform, you can pinpoint the exact places where the execution time stalls most and fix that.

  5. Web 2.0 .NET vs. LAMP Part 1: Blurring the Line pingbacked on November 21st, 2007 at 7:34 am
  6. #6 anthony responds...
    December 19th, 2007 at 12:02 pm

    WOW! I never knew this was true. Thanks!

  7. #7 Shelon Padmore responds...
    December 26th, 2007 at 8:07 pm

    Interesting,
    What’s more is that in most cases the performance gain from any such optimization is sometimes negligible when compared with the latency of rendering back to a browser across the web. Consider the context of application…

    - Shelon Padmore

  8. #8 minikperi responds...
    March 28th, 2008 at 4:58 am

    i wonder if cost of these methods on speed of php is too much why php’s development team devoleped these methods??? . i think if there is such problem they will fix it next versions of php.

  9. #9 stas responds...
    July 3rd, 2008 at 5:46 am

    It is just useful to keep these functions in mind. Programms becoming flexible this way.

    P.S. Many thanks for the post.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

  • Search

  • Subscribe

    Subscribe All Posts
    Subscribe All Comments
    Subscribe All Bookmarks
    Subscribe with Bloglines Subscribe with My Yahoo Add to netvibes Subscribe in NewsGator Online Add to Google
  • Share This

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (16)
    • Open Source (14)
    • PHP (95)
    • Software Design (28)
    • Usability (14)
    • WACT (7)
    • Web Design (20)
  • Recent Comments

    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  94
      noujoum, Michiel Van Kets, isorabins [...]
    • Sarah Snow Stever  24
      Dubai Web Design, Development, Snowcore, ennah [...]
    • Benchmarking PHP's Magic Methods  8
      stas, minikperi, Shelon Padmore [...]
    • Keywords and Language Simplicity  7
      olmse, Handy, minikperi [...]
    • Even the Big Guys Get Validation Wrong  4
      James Benson, Michael, Enric Naval [...]
    • Improved Error Messages in PHP 5  12
      James Benson, Iron, baggreeddog [...]
    • How to Transfer Mac OS X Application Data between Computers  36
      Secret Santa, micala, Khaled bin Alwaleed [...]
    • Programming Language Trends via Google  15
      MattW, Jeff Davis, retry [...]
    • PHP Development From Java Architects Eye  10
      Grrkkvho, Bobrila, FelhoBacsi [...]
    • The value of MVC  9
      Euvmetkk, Bobrila, Vulchak [...]
    • nofollow and comment spam  5
      Scopmazo, Qmumqckx, sss [...]
  • Pages

    • Tags
  • Recent Posts

    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
    • Keywords and Language Simplicity
    • Improved Error Messages in PHP 5
    • Michigan Taxes Graphic Design Services
    • Ruby versus PHP or There and Back Again
  • Archives

    • 2008: May
    • 2007: Jan Feb Mar Apr May Sep Oct Nov
    • 2006: Jan Feb Mar Apr May Jun Jul Oct Nov Dec
    • 2005: Jan Feb Mar Apr May Sep Oct Nov Dec
    • 2004: Apr May Jun Jul Aug Sep Oct Nov
  • Menu

    • Register
    • Log in