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,026 Comments

  1. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an nervousness over that you wish
    be delivering the following. unwell unquestionably come more formerly
    again as exactly the same nearly a lot often inside case you shield this hike.

  2. Undeniably believe that which you said. Your favorite reason appeared to be at the net the easiest thing
    to take into account of. I say to you, I definitely get annoyed
    whilst other folks consider concerns that they plainly do not know about.

    You controlled to hit the nail upon the top and outlined out the entire thing without
    having side-effects , other folks can take a signal.

    Will probably be again to get more. Thanks

  3. My brother suggested I may like this blog. He was once entirely right.
    This put up truly made my day. You cann’t believe simply how a lot time I had spent for
    this information! Thanks!

  4. Good day! I could have sworn I’ve visited this website before but after browsing through some of the
    posts I realized it’s new to me. Regardless, I’m certainly pleased I
    found it and I’ll be bookmarking it and checking back frequently!

  5. Normally I don’t learn post on blogs, but I wish to say that this write-up very compelled me to
    try and do it! Your writing style has been amazed me. Thank you,
    very nice article.

  6. Greate pieces. Keep writing such kind of info on your blog.

    Im really impressed by your site.
    Hey there, You’ve done an excellent job. I will certainly digg it and in my opinion recommend
    to my friends. I’m confident they’ll be benefited from this site.

  7. Today, I went to the beach with my children. I found a sea shell and gave it
    to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her
    ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!

  8. My developer is trying to convince me to move to .net from PHP.

    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am concerned about switching to
    another platform. I have heard great things about blogengine.net.

    Is there a way I can transfer all my wordpress content into it?
    Any kind of help would be greatly appreciated!

  9. Wow, superb weblog layout! How long have you been blogging for?
    you made blogging look easy. The whole glance of your site is magnificent,
    as smartly as the content material!

  10. Appreciating the commitment you put into your site and in depth information you provide.
    It’s awesome to come across a blog every once in a while that isn’t the same old
    rehashed material. Wonderful read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  11. Hello to every body, it’s my first visit of this web site;
    this webpage includes remarkable and actually good data designed for readers.

  12. Hello my family member! I want to say that this article is awesome,
    great written and come with approximately all significant infos.

    I’d like to look extra posts like this .

  13. fantastic publish, very informative. I’m wondering why the other specialists of this sector don’t understand this.
    You must continue your writing. I’m sure, you’ve a great readers’ base already!

  14. Thanks for another excellent article. The place
    else may just anybody get that kind of information in such a perfect approach
    of writing? I have a presentation next week, and I am at the search for such info.

  15. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get got an shakiness over that you wish be delivering the following.
    unwell unquestionably come further formerly again since exactly the
    same nearly a lot often inside case you shield this hike.

  16. I’m extremely pleased to find this web site. I want to to thank you for ones
    time for this wonderful read!! I definitely savored every little bit of it and i also have you
    book-marked to see new information in your website.

  17. Aw, this was a really nice post. Taking a few minutes and actual effort to
    generate a really good article… but what can I say…
    I hesitate a whole lot and don’t manage to get anything done.

  18. Have you ever thought about publishing an e-book or guest authoring on other
    websites? I have a blog based on the same information you discuss and
    would love to have you share some stories/information. I know my readers would value
    your work. If you’re even remotely interested, feel free to send me an e-mail.

  19. After I initially commented I appear to have clicked the
    -Notify me when new comments are added- checkbox and from now on whenever a comment is added I receive 4 emails with the exact same comment.
    Perhaps there is an easy method you are able to remove me from that service?
    Thank you!

  20. Hello there! Do you know if they make any plugins to help
    with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
    If you know of any please share. Appreciate it!

  21. Have you ever thought about creating an e-book or guest authoring on other sites?

    I have a blog based on the same topics you discuss and would love to have you share some stories/information. I
    know my audience would value your work. If you are
    even remotely interested, feel free to send me an e-mail.

  22. Hello just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading correctly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both show the same outcome.

  23. Does your blog have a contact page? I’m having problems locating it but, I’d like
    to shoot you an e-mail. I’ve got some suggestions for your blog
    you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time.

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

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.