Page 1 of 1

loop speed vs repeated statements

Posted: Mon Jan 14, 2019 12:31 am
by Ednixon
How does the execution time compare between a FOR loop with incrementing arguments, and repeating the code over and over? Looping certainly be the preferred approach, but would it run any faster?

Specifically, I have 3 IF statements that need to repeated about 50 times with changing arguments each pass. (A polling operation) Obviously writing the same basic code over and over entering the arguments as needed, would work, but would be cumbersome and inefficient. But how does the processing speed compare?

Re: loop speed vs repeated statements

Posted: Mon Jan 14, 2019 1:00 am
by jahboater
It depends if it fits in the cache or not.
Nowadays usually loops are preferred to unrolling.
The compiler at -O3 does not unroll loops much now.

You can get a detailed analysis of your programs use of cache with this tool:

http://valgrind.org/docs/manual/cg-manual.html

perhaps have your 50 sets of arguments in a lookup table indexed by the loop variable.

As usual measurement (benchmarking) will tell you if your optimizations are beneficial.

Re: loop speed vs repeated statements

Posted: Mon Jan 14, 2019 11:24 am
by przemof