Performance on arrays

It is a little bit machine specific question.

Which is better approach?

1
2
for (int x = 0; x < width; x++)
  matrix[x+width*y]=blabla;


or

1
2
for (int x = 0, offs=y*width; x < width; x++)
  matrix[x+offs]=blabla;


I believe the second, but if I remember well, both lines translated to only one x86 assembly instruction.

Am I correct?
Have both instructions the same clock ticks?

If it is true, we use first approach because it is more watchable.
I think that an optimizing compiler will realise that width*y is constant in the loop and optimize out the multiplication. Try it, generate the assembly and see.

I wouldn't worry about it though, if that's the slowest point of your program you're doing very well.
closed account (zb0S216C)
I personally would go for the 2nd option since the CPU doesn't have to re-compute y * width with every pass of the loop.

Wazzak
Topic archived. No new replies allowed.