- Is there a difference between the following two code blocks in terms of the resulting machine code when using the
llvmorgcccompilers? - When is this optimization actually worthwhile, if ever?
Not optimized:
for (int i=0; i<array.count; i++) {
//do some work
}
Optimized:
int count = array.count;
for (int i=0; i<count; i++) {
//do some work
}
EDIT: I should point out that array is immutable and array.count doesn’t change during the loop’s execution.
array.array.countis nearly always insignificant compared with “some work”. The way to measure it, though, is to use a profiler (or equivalent) and observe what proportion of your program’s runtime is spent at that line of code. Provided the profiler is accurate, that’s the most you could hope to gain by changing it.Suppose
array.countis something really slow, that you happen to know will always return the same result but the compiler doesn’t know that. Then it might be worth manually hoisting it.strlengets used as an example. It’s debateable how oftenstrlenis actually slow in practice, but easy to manufacture examples likely to run slower than they need to:You and I know that
some_functionnever returns 0, and hence the length of the string never changes. The compiler might not see the definition ofsome_function, and even if it does see the definition might not realize that its non-zero-returningness is important.