Classic POS

In a Coding Mood – A Classic POS Post

This is a classic post from my old blog that I’m porting over here. As I read through my old blog and find these in preparation to take it offline, I’ll be posting “Classic” POS (Pete on Software… not the other, more popular meaning) posts on here. This code came from my reading of Code Complete, 2nd edition.


Let me share something with you. It is assumed often that less code will result in faster execution of a program. Examine the following code written in C#.

for (int i=0; i <10;i++)
{
  someArray[i] = i;
}

versus

someArray[0] = 0;
someArray[1] = 1;
someArray[2] = 2;
someArray[3] = 3;
someArray[4] = 4;
someArray[5] = 5;
someArray[6] = 6;
someArray[7] = 7;
someArray[8] = 8;
someArray[9] = 9;

Both sets do the same thing. Most every programmer that I know will do the first one. But, the loop (first example) is 2.5 times SLOWER than the explicit declaration (second one) when run. In some languages the equivalent code is 4.5 times slower. Unrolling loops (where possible), is often a way to tweak code for speed when you’ve just got to have it.

There is always so much to learn.

Leave a Reply

Your email address will not be published. Required fields are marked *