Friday, April 29, 2011

Premature Optimization or Excessive Optimization

Premature Optimization refers to the process of optimizing incomplete code, or optimizing a program before that program is even finished.  Excessive Optimization refers to adding absurd amounts of optimization to a program that do not result in any noticeable gains.

I am bringing these two points up now because they directly relate to what is currently happening with the Project Euler problems. They are at a point, still early on, when a simple brute force approach will suffice. Is it possible to whip up an algorithm that will do it in a fraction of the time?  Sure.  Is it worth it when the amount of time saved will not even be noticeable unless you are taking benchmarks of the apps?  Not so much.  What about since the whole point of these exercises (at least in my opinion) is to get the ground work laid out showing what can be expected in terms of mathematical difficulty?  Even less so.

Many beginners to programming tend to worry far too much about getting a program to run as fast as possible. I have chatted with people that were trying to optimize their code so that the answer is given faster than the current speed of 0.5 SECONDS.  This is all well and good if used in a learning context, but if this is used in a production environment, then you just completely wasted your time since the end user won't even notice that change.  As with everything in this world there are exceptions, like if your program is going to be run several thousand times in a row, but that is just what needs to be taken into consideration here.

Optimization is a good thing and is fairly important in the high-speed world of today, but it's value fades if improperly used.  If there is a tool that you are going to use (on average) once a week for a couple of months and then dispose of it, is there really a point in spending an extra 2 hours to get it milliseconds faster?  Following are a couple of things that I look at when making a tool or application to decide what level of optimization is really needed (in no particular order):

  • Target Audience
    • Personal use
    • For my wife or other family member
    • For general release
  • Frequency of Use
    • One time use
    • Once a week
    • Once a day
    • Several times throughout the day
    • Several times, one right after the other
  • Work/Play
    • Am I being payed to make it?
  • Time Involved
    • Couple of minutes to streamline a couple of loops
    • Couple of hours to rewrite main logic
This is not an exhaustive list of course, just the main things that I run through in my head.  The target audience is pretty self explanatory. If I am making a quick tool for myself to go through 5,000 files and check that the version number is correct on them all I am not going to invest any time at all into optimizing.  If the program is going to be used in an automated build process by the company that I work for, I will optimize a little, but not much. If I am working on a game that I am planning to release to the general public I am going to optimize it as much as I can.  This ties directly into the frequency of use issue.  If I am making a tool that I am going to use once then erase optimizing is less likely to happen than if it is a program that is going to be (hopefully) used daily by millions of people all over the world.  This also ties loosely into the work/play scenario.  If I am doing something for my job I have to take into account how much time it will cost me to optimize and if that would be a good investment for my company to make. Everything that I code at work is costing the company money.  That is a perfect lead in to the last point, time involved.  If a program takes me 20 minutes to make and 90 minutes to optimize is that really worth it?  however if it takes me 5 hours to make and 5 minutes to optimize is that better?  Like I said though, these are just some of the things that I think about when it comes to optimization.  I would like to hear what others think about it though.

Until next time...
Enjoy!

0 comments:

Post a Comment