Day 16: Measure Performance Before Optimizing

Writing programs takes time, but writing correct and fast programs is even more difficult. That is why there is so much badly written software in the market. Bugs are the result of sloppy and rushed code. Programs are frequently slower than they need to be as a result.

In this situation, it sometimes seems smart to design systems that are efficient. But for many programmers, efficiency becomes an end in itself, and they end up trying to optimize code before understanding the real implications of these optimizations.

One of the most common problems is not understanding the real bottlenecks in the system. For example, when writing software for the web using standard architectures, accessing the data base is frequently the bottleneck of the system. When faced with this situation, it doesn’t matter if the code to handle string comparison is written in assembly or in Ruby, the result will be approximately the same.

Measuring Speed Up

There is an interesting result studied in computer architechture called the Ahmdal Law. This principle says that any optimization (say, getting twice the performance) applied to a part of the system that is responsible for only a small percentage of the execution time will result in a very small improvement.

For example, suppose that you improve the string handling code to be 10 times more efficient. This might give you hopes that the whole system will be a few times more efficient that it is. However, after measuring the system, you verify that the time spent on string comparison is 10% of the total time.

The result is that an operation that takes 10% of the total time now takes only 1% of the time. However, this means that the performance improvement for the whole system is just 9%.

If 9% still seems good for you, notice that I was very generous here. It is really hard to change something to be 10 times faster, unless you come up with a different algorithm. Also, the differences in percentage of usage are not commonly in the range 10 to 90%, but more frequently it is 1 to 99%. That is, most areas of your code contribute 1% or less to the running time, while bottlenecks such as network and database delays are responsible for 99% of the time.

Even for processor bound tasks this situation remains. There is probably a few small parts of your code that are responsible for 99% of the time spent in the program.

That is why measuring a system is so important. If you don’t know what is responsible for that figure of 99%, your efforts to improve the performance of the system are basicaly wasted. You should aim first at understanding what contributes to the bottlenecks in the system. Then, you can start planning on how to improve that area.

Further Reading

Read Computer Architecture: A Quantitative Approach to find everything you may want to know about Amdahl Law and how it applies to computer systems.

Similar Posts:

Similar Posts

1,038 Comments

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have
    some experience with something like this. Please
    let me know if you run into anything. I truly enjoy reading your blog and I look forward to
    your new updates.

  2. It’s in point of fact a great and useful piece of information.
    I’m satisfied that you simply shared this useful information with us.
    Please stay us informed like this. Thanks for sharing.

  3. Hi there! I could have sworn I’ve been to this website before but after browsing through some of the post
    I realized it’s new to me. Anyhow, I’m definitely
    happy I found it and I’ll be bookmarking and checking back often!

  4. We are a gaggle of volunteers and opening a brand new scheme in our community.
    Your site offered us with useful information to
    work on. You have done a formidable job and our entire
    neighborhood will likely be grateful to you.

  5. I was curious if you ever considered changing the layout of your blog?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful lot of text for only having 1 or 2 images.
    Maybe you could space it out better?

  6. I have been browsing online more than 4 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the web will be
    much more useful than ever before.

  7. Hi outstanding website! Does running a blog such as this take a lot of work?
    I’ve absolutely no knowledge of programming however I was hoping to start my
    own blog soon. Anyhow, should you have any suggestions or
    tips for new blog owners please share. I understand this is off topic nevertheless I just wanted to ask.

    Cheers!

  8. Hello very cool web site!! Guy .. Beautiful ..

    Amazing .. I will bookmark your web site and take the feeds additionally?
    I am happy to search out a lot of helpful information here within the submit, we
    want develop extra techniques in this regard,
    thanks for sharing. . . . . .

  9. The other day, while I was at work, my sister stole my iphone
    and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.
    I know this is completely off topic but I had to share it with someone!

  10. Wonderful beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog website?
    The account helped me a acceptable deal. I had been tiny bit
    acquainted of this your broadcast provided bright clear idea

  11. I am really impressed with your writing skills
    as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it’s rare to
    see a great blog like this one nowadays.

  12. Hmm it appears like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I’m still
    new to the whole thing. Do you have any recommendations for first-time blog writers?

    I’d genuinely appreciate it.

  13. When I originally commented I appear to have clicked
    the -Notify me when new comments are added- checkbox
    and from now on each time a comment is added I
    recieve 4 emails with the exact same comment.
    Perhaps there is an easy method you are able to remove me from
    that service? Appreciate it!

  14. Hello, i believe that i noticed you visited my site so i came to return the want?.I’m attempting to find issues to enhance my site!I guess
    its ok to make use of some of your concepts!!

  15. Hey! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really appreciate your content.
    Please let me know. Many thanks

  16. Very nice post. I just stumbled upon your weblog and wanted to mention that
    I’ve truly enjoyed surfing around your weblog
    posts. In any case I will be subscribing for your feed and I am hoping you write once more very soon!

  17. I blog quite often and I genuinely appreciate your content.
    This article has really peaked my interest. I’m going
    to book mark your site and keep checking for new information about once a week.

    I opted in for your RSS feed as well.

  18. Thank you for the good writeup. It actually was once a enjoyment account it.
    Glance complicated to far delivered agreeable from you!
    By the way, how can we be in contact?

  19. Hey there! I know this is kind of off topic but I was wondering which
    blog platform are you using for this website? I’m getting sick and tired of WordPress because I’ve had issues with hackers and
    I’m looking at alternatives for another platform.
    I would be great if you could point me in the direction of a good platform.

  20. Woah! I’m really enjoying the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s hard
    to get that “perfect balance” between superb usability and visual appearance.

    I must say that you’ve done a amazing job with this.
    Additionally, the blog loads extremely quick for me on Firefox.
    Excellent Blog!

  21. Greetings I am so thrilled I found your webpage, I really found
    you by accident, while I was looking on Yahoo for something else, Anyways I am here
    now and would just like to say kudos for a marvelous post and a all round exciting blog (I also love the theme/design),
    I don’t have time to look over it all at the moment but I have book-marked it and also included
    your RSS feeds, so when I have time I will be back to read
    much more, Please do keep up the great b.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.