CyberTech Rambler

April 24, 2006

Code Optimization Rule of Thumb

Filed under: Uncategorized — ctrambler @ 11:17 am

Russ Olsen wrote about the "Rules" for Java code optimization (or for any other language for that matter). To lend support to him, I will confess that what he said was true for my work. These are what I will add:

  • " Before you can make something work fast, you need to make it work. "
    • My rephrase for this: "People are willing to wait longer time for better results. There is no point in generating the WRONG results faster."
    • Olsen probably said it better latter in the article: "How fast is broken?"
  • "Rest assured, you don't know where the problem is"
    • This is always true. Takes his advice, profile your code first.
    • Perhaps most important is to understand the limitation of your profiling tools. In particular, uncertainty and error margin of each measurements.
    • Make sure you are profiling the code in the way you are expected to use it: There is no point optimizing code for tasks which are used once every year. Where human interaction with the profiled code is needed, they have to be taken into account. There are a lot of variables (beyond computing execution) that affects optimization. For example, although a subsection of code is painfully slow, if it is only to be executed between two human interaction events, e.g. two clicks, (i.e., it is constrained by human interaction time) it might not be worth the optimization effort than another subsection that is not as slow but require no human intervention.
    • Following on the previous point, try to put profiling the code as they are used. If you cannot afford to put profiling code in the actual code use when it is used, make sure your test case for profiling fit your usage well.
    • Think before you optimize the code: If the code is in flux (constantly changing), your optimization effort is going to be futile in the next revision of the code.
  • "If [new optimized code] doesn't help, take it out"
    • From an optimization viewpoint, this is true. The new code does not help, it does not belong there.
    • From a practical point of view, if you keep the new code in it,
      • It is less robust than the original, if only for the simple fact that the original is tested code
      • The original code is better understood by the programming team. There is no merits in introducing the new code since it does nothing and only one person understands it.
    • There is some arguments against archiving the new optimized code somewhere in the hope that it will be useful one day
      • [Especially if the code is to replace some extremely well-worn code] The original code is unlikely to be modified. Thus, no possible use for new optimized code.
      • The original code evolves with time. Hence, the optimized code obsolete.
      • Probably only the programmer knows about the optimized code.
    • However, I think there may be merits in keeping the new optimized code, not for optimization reasons, but for reasons such as better code organization, better architecture etc. This have to be weighted against the numerous disadvantages of "modifying a working version of the program code". Sometimes, the risk can be worthwhile.
    • "Know where to stop"
      • Before one begin, define the "Stop" criteria.
    • "But much of the time optimization is the process of taking nice code and making it longer, more complex, harder to read. In short, less nice."
      • Very true.
      • Sometimes, optimization become the "secondary" aim as the programmers elevate his personal aim of showing off to his colleague.
      • Optimization is generally fragile (any changes to the optimised code can undo the optimization effort) and require "maintainability" as the sacrificial offering to the optimization god. Good programming team weights optimization against maintainability and find the appropriate compromise.

 That's my two cents on optimization.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: