Posts: 23
Joined: Wed Jan 20, 2016 11:14 pm

loop speed vs repeated statements

Mon Jan 14, 2019 12:31 am

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?

Posts: 3786
Joined: Wed Feb 04, 2015 6:38 pm

Re: loop speed vs repeated statements

Mon Jan 14, 2019 1:00 am

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:


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.

Return to “C/C++”