0

I am new to the preprocessor, I rarely use it, so this question might seem silly. In my C program I discovered that a for loop which had a fixed start and end known at compile time was causing some performance issues. I temporarily solved this by manually rolling it out, however that seems to be a bit dirty. Is there a way to tell the preprocessor to turn this:

for(int i = 0; i < 8; i++)
{
    doSomeThingWithI(i, [...]);
    doSomeMoreStuffWithIt(i, [...]);
    foo();
    [...]
}

into this:

doSomeThingWithI(0, [...]);
doSomeMoreStuffWithIt(0, [...]);
foo();
[...]

doSomeThingWithI(1, [...]);
doSomeMoreStuffWithIt(1, [...]);
foo();
[...]

[...]

The important thing is that I have to use "i" inside the loop.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
l'arbre
  • 719
  • 2
  • 10
  • 29
  • 2
    Possible duplicate of [Writing a while loop in the C preprocessor](http://stackoverflow.com/questions/319328/writing-a-while-loop-in-the-c-preprocessor) – prazuber Feb 18 '17 at 16:56
  • 3
    I doubt the loop is your problem... – Jean-Baptiste Yunès Feb 18 '17 at 16:56
  • 1
    I expected this comment. The loop only contains few instructions, is however executed per pixel in a raycaster. The difference between the non-rolled-out and the rolled-out for loop in terms of performance measured in fps is about 10.8% – l'arbre Feb 18 '17 at 17:02
  • 1
    I don't think there is a portable/standard compliant way to instruct the compiler to unroll a loop. Loop unrolling is usually part of the optimization phase, so you can try (if you didn't already) to play around with the compiler optimization settings (check the manual of your compiler for options). Otherwise I'd just use the manually unrolled loop. – UnholySheep Feb 18 '17 at 17:08
  • 3
    _I am a preprocessor noob, I rarely use it,_ Please try to keep it that way, You end up with more readable and debuggable code – Ed Heal Feb 18 '17 at 17:08
  • Perhaps it is a better bet to get some stats what is taking the time/memory. I would guess it is the contents of the loop not the loop itself – Ed Heal Feb 18 '17 at 17:28
  • @EdHeal The preprocessor is crucial for C code that must compile in multiple environments. C is not standardized like Java and C#. – nicomp Feb 18 '17 at 17:32
  • @nicomp - I agree but get it to do stuff like compiling bits of code. Noting too complicated. Use the compiler for that – Ed Heal Feb 18 '17 at 17:35
  • @EdHeal if by content you mean the "body" of the for loop, then no, it's the unnecesary jumps and comparisons and increments of i – l'arbre Feb 18 '17 at 17:41
  • If you want to optimise: make the functions inside the loop static , causing them to be inlined. The loop machinery is nothing compared to the functioncall overhead. – wildplasser Feb 18 '17 at 18:33
  • @EdHeal I see what you mean. Point taken. – nicomp Feb 18 '17 at 18:42

1 Answers1

2

You could always hard-code it like this (replace printf with your functions):

#include <stdio.h>

#define LOOP_8(X) X X X X X X X X

int main() {
   int i = 0;
   LOOP_8(printf("Loop number %d\n", i++);)
}

But that's probably way too ugly to use in real code.

cfromme
  • 470
  • 4
  • 11