Loop without comparison n times in C

If I have a loop that I know should run n times, is there a way to write a while (or for) loop without comparing each time? If there is no way to make a rotation macro:

int i = 0; for(i = 0; i < 5; i++) { operation(); } 

in

 operation(); operation(); operation(); operation(); operation(); 

PS This is the fastest cycle that I have come up with so far.

 int i = 5; while(i-- >= 0) { operation(); } 
+5
source share
5 answers

Enough Smart Compiler will do it for you. More specifically, compiler optimization understands loop reversal. This is a fairly simple optimization, especially in cases such as your example, when the number of iterations is known at compile time.

So, briefly: enable compiler optimization and don't worry about it.

+7
source

The number of instructions that you write in the source code is not strictly related to the number of machine instructions that the compiler generates.

Most compilers are more intelligent, and in your second example, it can generate code like:

 operation(); operation(); operation(); operation(); operation(); 

automatically because they detect that the cycle will always be repeated 5 times.

Also, if you perform profiling-oriented optimization, and the compiler sees that the loop has a tiny body and a very high repeat counter, it can expand it even for the total number of iterations with code like:

 while (count >= 5) { operation(); operation(); operation(); operation(); operation(); count -= 5; } while (count > 0) { operation(); count--; } 

This will result in a large count about one fifth of the tests compared to the naive version.

If it's worth it or not, this is something that only profiling can describe.

One thing you can do if you know for sure that the code must be executed at least once is to write

 do { operation(); } while (--count); 

instead

 while (count--) { operation(); } 

The possibility that count==0 somewhat annoying to the processors, because in the code generated by most compilers, an additional JMP is added forward:

  jmp test loop: ...operation... test: ...do the test... jne loop 

machine code for version do { ... } while - it's just

 loop: ... opertion ... ... do the test... jne loop 
+5
source

both loops will perform comparisons.

in any case, the compiler must identify the constant iteration and deploy the loop.

You can verify this with gcc and the optimization flags (-O) and then look at the generated code.

More important: Do not optimize if there are no substantial reasons!

0
source

After compiling the C code, the while and for loops are converted into comparison operators in machine language, so there is no way to avoid any comparison with for / while loops. You can do a series of goto statements and arithmetic that avoid using comparisons, but the result is likely to be less efficient. You should see how these loops are compiled into machine language using radare2 or gdb to see how they can be improved there.

0
source

With a template, you can expand the loop (known in the graph at compile time) with something like:

 namespace detail { template <std::size_t ... Is> void do_operation(std::index_sequence<Is...>) { std::initializer_list<std::size_t>{(static_cast<void>(operation()), Is)...}; } } template <std::size_t N> void do_operation() { detail::do_operation(std::make_index_sequence<N>()); } 

Live demo

but the compiler may already perform such an optimization for a normal loop.

0
source

Source: https://habr.com/ru/post/1210383/


All Articles