Computer Science Canada [Tutorial] Premature Optimization... Don't! |
Author: | wtd [ Sat Oct 02, 2004 6:01 pm ] |
Post subject: | [Tutorial] Premature Optimization... Don't! |
This really belongs in General Programming, but most of the tutorials activity is here, so this seems the most likely place for it to be seen. Optimization can be a good thing in a computer program. It makes that game run a few frames per second faster, or the calculator chew a little less RAM. However, it's critical to understand optimization. Ultimately, the key to optimiztion is information about what the program is actually doing. Whether it's in your head, or in a compiler, optimization can only occur because someone knows what's going on and can think of a quicker way of getting the same thing done. Th optimization this tutorial is concerned with is the optimization we as humans do. You look at a piece of code and you realize that a few lines are unnecessary or redundant in the grand scheme of things and you get rid of them. The catch is that when you do this, you remove information from the source. The more information you remove from the code, the harder it is to figure out what's going on, and thus the harder it is to perform further optimizations. So, it turns out that the best way to optimize a program is to not do so until you have it finished. This way you have the most information at your disposal and you can make the greatest number of optimizations. Remember, when you write code, you're writing it for yourself or others. If you were writing it for the computer, you'd directly input machine code. Of course even that is becoming a thing of the past. Java, for instance, can already be optimized by the Java Virtual Machine to run nearly as fast or even faster than natively compiled C++ programs. This is because the Java compiler preserves quite a bit of information about the program. That information can then be used to figure out faster, more efficient ways of getting things done. So remember, write the code so you can understand it, then when you're done, if you can, optimize it. The greatest speed gains you'll get are by improving the design of your programs, and you have to be able to read your programs to do that. |