Code performance

Code performance is one of the most polemics topics in software development process, addressing a lot of issues, for example:

  • When to improve code performance?
  • Should I prioritize legibility or response time?
  • What part of my code should be improved?

There is a lot of discussion about code performance and one reason for that is due to fast code isn’t the best code. Code optimization generally affects code legibility, which affects code readability, which affects code maintenance, and so on. So, when you realize that is time to look a bit deeper to your code execution time, you must be aware of some trick aspects.

The first one is the Pareto Principle (also known as the 80-20 rule) which states that, for many events, roughly 80% of the effects come from 20% of the causes. This is exactly what happens in code optimization: 20% of the routines are responsible for 80% of program’s execution time. Sometimes you take the whole afternoon optimizing a loop which seems to be the cause of the slowness of your program. And after using all your optimization skills, the program looks like the old one. To avoid this kind of situations you should first measure your program’s bottleneck using a profiler tool: remember the 80-20 rule. After finding it and re-writing it using some basic optimizations techniques, you will notice that your code will be 80% faster.

Another tip is not relying on what everyone says about code optimization (me included). Have you ever heard about reducing lines of code, optimizing while coding, or this operation should be faster the other? Well, they are all suspicious.

Lot of programmers thinks that writing routines using just one or two lines of code makes a faster program. Take a look at the code below taken from Code Complete book.

For i = 1 to 10
a[i] = i
end

a[1] = 1; a[2] = 2; a[3] = 3;
a[4] = 4; a[5] = 5; a[6] = 6;
a[7] = 7; a[8] = 8; a[9] = 9;
a[10] = 10;

Both programs do the same thing. Which one do you think is faster? These programs were compared in the book and here is the result:

For loop
– Visual Basic: 8,47s
– Java: 12,6s

Linear code
– Visual Basic: 3,16s
– Java: 3,23s

In this case, the linear code was more than 60% faster than the “for loop”. Does it prove that as longer as your code, faster it will be? No! This proves that numbers of line and code performance does not have any predictable relationship.

Another very common mistake is optimizing your code while writing it. It is almost impossible to find bottlenecks without any specific tool. Without a profiler, you will probably lose time trying to optimize code that really doesn’t matter for your program performance. In addition, optimizing code while programming take you away from the main focus of it, for example user interaction and legibility. Premature code optimization is generally a bad choice.

So, when optimize code? First, concentrate efforts in creating modular, maintainable program, always aware of making things easy for future changes. When it’s done, measure its performance and optimize if necessary, but only if necessary. As small parts of code consumes a considerable percentage of the whole program’s execution time, measure your code before the modifications and just after them, so you can keep track if your changes are really effectives. Just to illustrate the difference between a readable code and an optimized code, see both examples below:

sum = 0;
for (row = 0; row < rowCount; row++) {     for ( column = 0; column < columnCount; column++) {          sum = sum + matrix[row][column];     } } [/sourcecode] [sourcecode language='cpp'] sum = 0; elementPointer = matrix; lastElementPointer = matrix[rowCount-1][columnCount-1]; while (elementPointer < lastElementPointer) {     sum = sum + *(elementPointer++); } [/sourcecode]

Both loops calculate the sum of matrix’s elements. However, the second one uses just sum operations when calculating memory address dislocation, while the first loop uses multiplication, much more expensive (don’t rely on me, go ahead and prove it by yourself). Although slower, the first code is much easier to read and modify. So, which code to use? Remember, write readable code first, measure it and, if necessary, optimize it.

Code complete also comes with an entire chapter dedicated just to code optimization techniques. Next posts I’ll talk about some of them. Hope you like.

See you,


Fernando

One thought on “Code performance

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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