views:

101

answers:

3

I'm writing a few very tight loops and the outermost loop will run for over a month. It's my understanding that the less local variables a function has, the better the compiler can optimize it. In one of the loops, I need a few flags, only one of which is used at a time. If you were the proverbial homicidal maniac that knows where I live, would you rather have the flag named flag and used as such throughout or would you prefer something like

unsigned int flag;

while (condition) {

#define found_flag flag
  found_flag = 0;
  for (i = 0; i<n; i++) {
    if (found_condition) {
      found_flag = 1;
      break;
    }      
  }
  if (!found_flag) {
     /* not found action */
  }

/* other code leading up to the next loop with flag */
#define next_flag flag
  next_flag = 0;
/* ... /*  
}

This provides the benefit of allowing descriptive names for each flag without adding a new variable but seems a little unorthodox. I'm a new C programmer so I'm not sure what to do here.

+10  A: 

Don't bother doing this, just use a new variable for each flag. The compiler will be able to determine where each one is first and last used and optimise the actual amount of space used accordingly. If none of the usage of the flag variables overlap, then the compiler may end up using the same space for all flag variables anyway.

Code for readability first and foremost.

dreamlax
+3  A: 

I completely agree with dreamlax: the compiler will be smart enough for you to ignore this issue entirely, but I'd like to mention that you neglected a third option, which is rather more readable:

while (something) {
  /* setup per-loop preconditions */
  {
    int flag1;
    while (anotherthing) {
      /* ... */
    }
    /* deal with flag found or not-found here */
  }

  /* possibly some other preconditions */
  {
    int flag2;
    while (stillanotherthing) {
      /* ... */
    }
  }
}

which would tell a dumb compiler explicitly when you are done with each flag. Note that you will need to take care about where you declare variables that need to live beyond the flag-scope blocks.

dmckee
Explicit scoping has become less popular now that C allows mixed declarations and code, but I still find myself using it every now and then.
dreamlax
@dmckee thanks for the trick. I didn't know about explicit scoping.
aaronasterling
You only need to do this in C89, not in C99 or C++ where you can declare a variable in the middle of blocks.
starblue
@starblue: the point (if aaron needed it on account of a really dumb compiler or something) would be to tell the compiler where you are *done* with a variable (i.e. when it goes out of scope) which is not something that the c99 mid-block declarations help with. As dreamlax said, the use of explicit scoping to declare variable in mid-block is consigned to the old standard, but they still have their uses.
dmckee
+3  A: 

Your trick would only be useful on very old, very simple, or buggy compilers that aren't capable of correct register (re)allocation and scheduling (sometimes, that's what one is stuck with for various or ancient embedded processors). gcc, and most modern compilers, when optimizations are turned on, would reallocate any register or local memory resources used for local variables until they are almost hard to find when debugging at the machine code level. So you might as well make your code readable and not spend brain power on this type of premature optimization.

hotpaw2