Maggie Nelson

Optimization Woes

by maggie on Mar.10, 2009, under entry

I think by now everyone’s familiar with the phrase:

“Premature optimization is the root of all evil.”

In my day-to-day job, I worry a lot about writing high-performance applications, especially in the way applications manage and retrieve data. There is a huge difference between designing your application to be optimized for a specific purpose and premature optimization. It’s one thing to worry whether echo or print is faster (who cares, really?) – but it’s another to design an application to tolerate some slowness in low-traffic parts but gaining ultra fast response time on the first access to the application. For example, viewing your photo album may take a while but seeing the homepage is pretty freakin’ fast!

In a recent post, Sebastian Bergmann points out that you should not micro-optimize (in reference to a PHP micro-optimization tricks post by Alex Netkachov). I agree wholeheartedly. It’s a nice list and definitely something to keep in mind, but it should be just a small component of your developer bag of tricks! Before you can optimize, you have to design well first. Proper design leads to potential for great optimization. And to design well, you have to really understand the problem you’re trying to solve. Sebastian links to slides by Ilia Alshanetky from PHP Quebec 2009 on Common Optimization Mistakes. Ilia also points out that it’s important to understand before you start fixing:

Solve the business case before optimizing the solution.

For PHP Advent, I wrote an article Optimize This!, which talks about how to optimize an existing application. The most important part to focus on is understand what the application is doing and then figuring out where you’ll have most impact. Sure, you can use echo over print (or print over echo, once again, who cares?) to get that 0.0001% performance boost, but if you’re doing 140 live SQL queries on your homepage, your application will still have 3 minute response time and with more users, the performance will continue to degrade until the application becomes unusable.

Modularizing code (aka “Object-Oriented Programming”) makes it easy to focus on problem areas in an existing application. Designing well doesn’t always mean designing for high performance. Instead, it means designing for flexibility and atomicity – you want to be able to easily change things without huge impact on the overall system. So before you go an optimize, take a step back and think about what your application is doing and what it’s supposed to do. Go write on the white board – diagrams prove very useful in figuring out flows and functionality! Go chat in #phpc channel on irc.freenode.net – the PHP community will help (with a little bit of playful making fun of, but we’ll help). Just don’t go changing all your methods to static methods unless you’re really sure it’s the right thing to do!

:, , , ,
7 comments for this entry:
  1. tedder

    It’s amazing to see the difference in how optimization is dealt with in industry versus in academia. Yes, optimization is important, knowing how to find the slow points are important (see Amdahl), but just as important is knowing when NOT to optimize or when to throw hardware at a solution.

    A good example was when it would take 20 hours to run through a daily log or dataset at our previous company. Yes, if you could move that to 12 hours or 4 hours you would come ahead. However, if it took 3 minutes to run through a day’s worth of data, it didn’t really matter. Developer time is a cost; “spend” it where you need.

  2. Paul M. Jones

    “Designing well doesn’t always mean designing for high performance. Instead, it means designing for flexibility and atomicity – you want to be able to easily change things without huge impact on the overall system.”

    Preach the Word, sister.

  3. Matthew Purdon

    Hey Maggie, I totally agree with your sentiments. I think the real issue with that quote is that some of its meaning has been lost by people shortening it. The original quote is thus:

    “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”

    I feel very strongly about the fact that people make design decisions based on efficiency over ease of development. Sometimes I wonder if they stick to a more procedural way of developing because they don’t actually grok OOP. It’s hard to see the benefits of object simplicity until you actually try to write things that way.

    Statements such as “I don’t want to create too many objects” or “I use static methods for performance” are what keeps PHP out of The Enterprise. It has little to do with PHP itself.

  4. The five immutable rules of optimization » Wahrnehmungen aus zwei Welten

    [...] the article PHP micro-optimization tips on his blog. This article has already stirred up some reactions. I want to put some more oil in the fire: while there is nothing wrong with using some of Alex [...]

  5. Les

    You can still modulise using Procedural Programming but I agree with the point your making – using OOP is the way to go for truly wonderfully decoupled code, etc.

    Beside, does any professional web developer *still* develop using Procedural methods today?

    Come on… ;)

  6. Stop Sacrificing Readability For Efficiency! | BrandonSavage.net

    [...] argued that micro optimization was a bad idea, from the perspective of unit testing, the idea that “premature optimization is the root of all evil”, the fact that it takes too much time, and that it violates the rules of development and [...]

  7. “Micro” Optimizations That Matter | BrandonSavage.net

    [...] of your application, rather than implementing these or any other strategies. You should always solve the business case for optimizations, too. Make sure you know why you’re doing them, and what you’ll gain. [...]

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Tags

RSS Planet PHP

  • Getting started with the Midgard content repository
    I'm doing a talk today in the Bossa Conference about using Midgard as a content repository for mobile applications. As part of my presentation I wrote some simple example code for using the Midgard APIs in Python, and thought they would be good to share to those not attending the event as well. The idea of a content repository is that instead of coming […]
    Henri Bergius
  • Neural Networks in PHP
    Neural Networks in PHP By Louis Stowasser Neural networks allow emulating the behavior of a brain in software applications. Neural Networks have always had a too steep learning curve to venture towards, especially in a Web environment. Neural Mesh is an open source, pure PHP code based Neural Network manager and framework that makes it easier to work with […]
    PHP Classes
  • CMS Watch on their Midgard usage
    Which CMS does The Real Story Group Use? (Tony Byrne / CMS Watch): The answer is, we use an open-source platform called "Midgard." We picked it nearly ten years ago, and it has held up fairly well.... One of the things we like about Midgard actually makes it rather unsuitable for many simpler publishing scenarios: it is highly object-oriented. This […]
    Henri Bergius
  • Upgrades In Open Source
    PHP 5.3 has been out now for eight months, and in that time lots of projects have made decisions to begin developing against this version of PHP. Juozas Kaziukenas makes the argument that you shouldn’t be afraid of PHP 5.3 and he provides a number of excellent points to support his argument. I don’t dispute [...] […]
    Brandon Savage
  • Seed doc updates, and Gio async directory listing
    The documentation for seed gobject introspection is improving continually, I now have a jhbuild virtual machine, which is picking up the latest versions from git.In addition many of the documentation details have been expanded, includingInterfaces, Enumscallback methods are now documentedMore libraries have been added.More doc comments can be picked upWith b […]
    Alan Knowles