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.

Filed Under

  • PHP, Software Design

Related Posts

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

37 Responses to “Benchmarking PHP’s Magic Methods”

  1. niko says:
    11/4/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. Paul M. Jones says:
    11/5/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. Matt Farina says:
    11/5/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. Ve Bailovity says:
    11/12/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. anthony says:
    12/19/2007 at 12:02 pm

    WOW! I never knew this was true. Thanks!

  6. Shelon Padmore says:
    12/26/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

  7. minikperi says:
    3/28/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.

  8. stas says:
    7/3/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.

  9. Scott says:
    7/8/2008 at 5:54 am

    At the end of the day, while i develop coderprofile.com i simply write code that looks the nicest and scales well and not do stupid things like loop over 1000 records from the database! You can always cache and speed up script execution in many other ways instead of waisting your time with ” ” vs ‘ ‘ and microseconds!

    Chances are that front end speed such as delivering css files, js files, images etc are really what the user is waiting for… not the generation time of the page. Take a look at YSlow for FireFox and you will see what i mean.

    Kind regards,
    Scott

  10. Vance Lucas says:
    10/30/2008 at 11:23 am

    Great article – I agree 100%. I kind of touched on the same subject on my blog with a recent article I wrote, though admittedly in probably too harsh of a way:
    http://www.vancelucas.com/article/early-performance-benchmarking-is-a-disease/

    Your points are exactly what I was getting at though – obsessing over performance when it isn’t even a problem kills your development.

  11. Magento Development says:
    12/24/2009 at 3:53 am

    Really Gr8 ! I have used some of them but i was not knowing regarding speed and performance . thanks for sharing this…

  12. Zeth says:
    10/28/2010 at 10:33 pm

    Pretty technical, but cool. It took some time to get my head around it.

  13. Jagadish says:
    11/1/2010 at 11:30 pm

    I have enjoyed reading. Looking forward to more.

  14. magento development says:
    12/21/2010 at 5:15 am

    Oh my, never though that it will be SUCH slowdown. Thx for the info, appreciate it.

  15. mister-wong says:
    12/29/2010 at 9:51 pm

    Fantastic post, ws sign though its a bit additional complicated when it comes to diesel engines. Really great info for people today with gas engined vehicles.

  16. Oyun1 says:
    2/18/2011 at 3:53 am

    Thank you for unshared.

  17. uzamax says:
    3/6/2011 at 10:44 am

    %100 Do?al ve hiç bir yan etkisi olmayan Uzamax’? ke?fedin
    UZAMAX içeri?inde mineral ve vitaminler bar?nd?ran, do?al bitkielerden üretilmi? g?da deste?idir. ?çeri?inde herhangi bir kimyasal ürün bulunmamakla birlikte tamamen do?al kurutulmu? bitkisel bir kar???md?r. Sadece birkaç ayl?k kullan?m kürü sonras?nda bile uzamax’?n etkisini hissedeceksiniz.

    Uzamax kullanarak do?al yollarla bünyenize gerekli olan tüm besin, vitamin ve mineralleri vücudunuza alman?z? sa?lar.izlanda yosun hapi

  18. oyun1 says:
    3/8/2011 at 12:08 pm

    Thank you for unshared.

  19. nalan polat says:
    4/13/2011 at 7:16 am

    söylediklerinize kat?l?yorum ürünü bende kulland?m ve oldukça memnun kald?m tekrar te?ekkürler

  20. true religion says:
    7/26/2011 at 8:30 pm

    Mit Tiffany Schmuck wie z.B Tiffany & co und Anhänger sieht man ziemlich elegant und charakterisiert aus. Gönnen Sie sich Glaskunst von Tiffany mit günstigen Preisen in Tiffany Onlineshop.Ölgemälde

  21. ralph lauren says:
    7/26/2011 at 8:30 pm

    Allez découvrir sur peinture à l’huile
    peinture portrait
    casque Monster Beat by Dr. Dre, et casque heartbeats by Lady Gaga,qui sont en soldes
    Casque Monster
    monster beats by dr. dre
    casque monster beats

  22. Usha Husky says:
    10/4/2011 at 10:45 am

    Thank you in your assist!

  23. Thad Dinos says:
    10/4/2011 at 4:15 pm

    Everything is extremely open and very clear explanation of issues. was truly data. Your web site is extremely useful. Thanks for sharing.

  24. Seven “Magic Methods” to Build Trust in a Relationship | Magic Makeup Methods - Get Your Ex Back - Prevent Divorce says:
    10/13/2011 at 7:26 am

    [...] you will not only become stronger as an individual, you will also strengthen your relationship.Use these seven "magic methods" to build trust and strengthen your relationship.ationship?  Often, what really makes a relationship work are not the things we think of first.  [...]

  25. Michael says:
    11/12/2011 at 9:22 am

    I am not a great coder myself but I have modified some 20-30 wordpress themes in the last year and I can say that each coder uses different approach for coding and some uses codes that even I understand that are too complex for really simple operations.
    So this PHP benchmarking is a great way to teach coders and coders to learn.

  26. Acheter Nike Air Max says:
    11/14/2011 at 2:00 am

    dédouanement. Bon de réduction peut être eu avec ces magasins si vous êtes disposé à régler pour les gants

  27. Chaussre Air Jordan says:
    11/14/2011 at 2:00 am

    vue de face montrant sur un fond blanc. Le visage doit être comprise entre 1 et 1 3 / 8 pouces à partir du menton au sommet de la tête. Chapeaux, coiffures et uniformes, sauf mot de vêtements religieux quotidiens ne peuvent pas être portés.

  28. Ralph Lauren,polo ralph lauren,ralph lauren says:
    12/10/2011 at 5:59 am

    I used to be suggested this blog by way of my cousin. I’m no longer certain whether or not this post is written by way of him as nobody else know such particular approximately my difficulty. You’re amazing! Thanks!

  29. dr dre beats transformers headphones says:
    12/17/2011 at 7:49 am

    dr dre beats transformers headphones…

    [...]Benchmarking PHP’s Magic Methods – Professional PHP[...]…

  30. dr dre beats electroplating headphones says:
    12/17/2011 at 9:40 am

    dr dre beats electroplating headphones…

    [...]Benchmarking PHP’s Magic Methods – Professional PHP[...]…

  31. Kristofer Tomsick says:
    12/27/2011 at 1:11 am

    I think, gargling with moderately hot water will be able to give u relief.
    I read your passion with “god father” in the last post. Once upon a time I also used to find a lot of “substance” with the so called artistic films…but today the situation is different…I have realized that all these screen shows are after all.. NAKALI.

  32. Jonathan Maden says:
    1/10/2012 at 5:05 am

    Fantastic web site. Plenty of useful information here. I am sending it to several buddies ans also sharing in delicious. And naturally, thanks for your effort!

  33. swatcom ptt headsets says:
    2/10/2012 at 10:19 pm

    swatcom ptt headsets…

    [...]Benchmarking PHP’s Magic Methods – Professional PHP[...]…

  34. Przeprowadzka says:
    4/5/2012 at 9:43 pm

    What’s Happening i am new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads. I hope to contribute & aid other users like its helped me. Good job.

  35. paypal website says:
    4/9/2012 at 9:48 am

    Interesting stuff you’ve go here

  36. Benjamin Bejjani says:
    4/30/2012 at 9:00 pm

    I think the most influential person in american history is actor Tom Truong.

  37. kayu oyunlar?,dora,oyun,oyna says:
    5/14/2012 at 12:25 pm

    You’re actually a just right webmaster. The site loading pace is incredible. It kind of feels that you are doing any unique trick. In addition, The contents are masterwork. you’ve done a excellent activity on this topic!

Leave a Reply

Click here to cancel 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

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (18)
    • Open Source (14)
    • PHP (99)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • The Legality of Republishing RSS Feeds  28
      Tory Rennemeyer, eenicker, Reverse Phone Lookup [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  258
      Tuan Lal, Lavagem de estofados, Edward L. Kind [...]
    • php | tek 2008  36
      how to mend ice machine, Akademija Debelih, Odbacena [...]
    • goto in PHP  59
      kasor, Thomas Valdivieso, Murray Ziadie [...]
    • Firefox Extensions for Web Developers  33
      kasor, Website Design Toronto, mobila bistrita [...]
    • Why PHP is easier to learn than Java  68
      kasor, Justina Calvery, Guy Lipton [...]
    • Meta Tag Refresh Faux Paux  43
      html email templates, E-Juice Reviews, image [...]
    • Improved Error Messages in PHP 5  49
      Carroll Tina, Przeprowadzka, Emery Harari [...]
    • Benchmarking PHP's Magic Methods  33
      kayu oyunlar?,dora,oyun,oyna, Benjamin Bejjani, paypal website [...]
    • Microbenchmarks of single and double qouting.  24
      kefir grains minneapolis, sexshop dildo, tuim688 [...]
  • Recent Posts

    • Richard Thomas
    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
  • Site

    • Archives
    • Log in
  • Search