Do Not Micro-Optimize

Sebastian Bergmann » 10 March 2009 » in PHP » 5 Comments

Whenever I read blog postings such as that one (in the spirit of John McCain's that one reference to Barack Obama) I get a little bit angry.

"calling a static method is faster than an object method"

It is called Object-Oriented Programming. Not "Class-Oriented Programming". Nor "Procedural Progamming with Classes as Namespaces".

The small performance gain you may or may not get at deployment-time by using static method calls instead of using instance method calls comes at the price of problems at development-time.

One of the problems at development-time that you are digging yourself into is that of untestable code: static methods are death to testability. Use them when it makes sense to use them and not because you think you can improve the performance of your website by using them.

I bet that your problems lie elsewhere when you think that you can solve them by a micro-optimization such as this. Have a look at Ilia's Common Optimization Mistakes slides for more information on this topic.

Defined tags for this entry: , ,

Trackback specific URI for this entry

5 Comments to "Do Not Micro-Optimize"

Display comments as (Linear | Threaded)
  1. Aaron James
    10/03/2009 at 14:57 Permalink
    Timely blog Sebastian, it was good to chat briefly with you after your PHP London Conference talk too :)

    I think the key point to consider here is that these days hardware and performance is getting cheaper and cheaper. Fixing code problems is getting more and more expensive.

    Sacrificing good coding practice for barely noticeable performance gains seems like lunacy to me.

    Reply

  2. Brandon Savage
    10/03/2009 at 16:26 Permalink
    I agree with your take here. While some of the suggestions are well known (avoiding warnings and notices, selecting only the fields you need), his inexperience is betrayed by his other suggestions. The little bit of extra time you get won't be worth it.

    Reply

  3. Giorgio Sironi
    10/03/2009 at 17:34 Permalink
    In my opinion "That one" is a collection of "How to make code maintenance impossible" tips. Some years ago, as a junior developer, I struggled to substitute " to ' in my project. Then I discovered apc...

    Reply

  4. Les
    13/03/2009 at 15:27 Permalink
    I agree too however it beggers believe the state that the Zend Framework is currently in at the moment, as it is filled with static calls - they're everywhere, and to think there are applications built upon this framework without a though given to the quality of the framework.

    I hear so much, "It's from Zend, so it must be good...", etc but has any serious PHP developer actually taken a good look under the hood recently?!

    Christ, I hope I ain't gonna be around when the sh*t hits the fan on this one :eek:

    Reply

  5. Jeremy Simkins
    23/10/2009 at 16:09 Permalink
    I only partially agree with this. While replacing hardware may be an easy and cheaper solution, in some cases, I want to argue that this is not always the case. I don’t use frameworks, too many issues to fix and I like to ensure none of my code will return any PHP warning, errors, or notices. This is my way of developing and I know every developer has his or her preferred method.
    I do not disagree with the article as you so plainly do. I find that those articles are great for getting beginners to code properly from the start to ensure cleaner, better, and hopefully faster code. As a duct-tape programmer, I have spent much of my time working on platforms of other programmers. I don’t want to get into detail but if a few of the concepts in the provided article were used the system would have ran much smoother. The system I’m using as an example is about 1.4GB in size. I can admit that the majority of the issues in the platform are that there is zero documentation (Fun for me, and that’s sarcasm), horrible SQL queries, and poorly written code. I do agree that SQL queries contribute to the penalty of speed; I want to argue that these concepts are not entirely bad. They are still good standards to be followed by any beginner. By going through the entire system and changing double quotes to single quotes, has noticeably improved performance. For most projects I would agree with you, but for larger projects, I kindly disagree.
    Replacing hardware is a nice solution, and in many cases it is enough. While this may be the cheaper and faster solution, this is not the best. Replacing hardware works, but it doesn’t fix the bigger issue, which can be fixed by cleaning and optimizing the code.
    Micro-optimize is not a bad thing, its just good logic to follow for beginners. The experts will learn/know when to use it or not.
    This is my look on this as someone who has once been a self-taught beginner. Almost ten years of PHP now has taught me this. While I am working on my ZCE currently, I do want to say I am not a fan of Zends’ current framework, and I agree with Les about the quality of the framework. Asides from my areas of disagreement, nice post.

    Reply

0 Trackbacks to "Do Not Micro-Optimize"

  1. No Trackbacks

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.