[Solved] Slower if temp variable is used for indexing?


I am interpreting the following to constitute your question.

If writing a speed-intensive app that involves lots of calculations as
such, should I rely on the optimization by compiler or on register? If
multi-thread, will register cause any problem?

First of all, if you are aiming for (and trying to measure) efficiency of a program, you should always be running with at least -O2 if not -O3, depending on what is used in production.

After the compiler has done everything it can for you, you can then consider hand-optimizations such as using register variables. Most of the time, you will find that the register keyword is either ignored by the compiler, or does not help your performance significantly.

The short answer to the question of whether multithreading will cause any problems with the use of register is no. Having said that, there are many ways to write incorrect multithreaded programs even without register.


Related issue mentioned in the question: Why are your timings all close to zero when you run with optimizations on?

If the compiler can determine through static analysis that either the result of the loops is never used, or the loops themselves can be replaced by a simpler computation (perhaps adding COUNT directly), then it will go ahead and do that, which will tend to cut down the time substantially.

The only way to know for sure is to use gcc -S -O2 to show the assembly after a particular optimization, and determine from the assembly whether the loops are still there.

solved Slower if temp variable is used for indexing?