4

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.

5
  • 1
    when in doubt, measure (or look at the generated assembler). Commented Aug 27, 2010 at 5:00
  • and yes, horribly, painfully, bite you in the posterior unorthodox Commented Aug 27, 2010 at 5:01
  • You'd be saving around 4 bytes of space per flag variable, and that's if you discovered your compiler didn't optimise the space. Commented Aug 27, 2010 at 5:07
  • @dreamlax What I read was that this will make it more likely that the compiler just sticks everything in a register which is what I want but I can see that it's a bad idea. It says I have to wait a few minutes before I accept your answer but consider it done. Commented Aug 27, 2010 at 5:09
  • 2
    @aaronasterling: If the compiler can see that the usage of each flag variable never overlaps and is used in a manner that a register would be most suitable for, then chances are it will use a register. A lot of effort goes into a compiler's optimiser. Also, you're doing pretty well for being new to C. I remember thinking the same question back when I was beginning C. Commented Aug 27, 2010 at 5:15

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

From @dreamiax 's lips (or rather keyboard in this case) to God's ear, stronger truths were never uttered. Please don't do this!
3

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.

3 Comments

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.
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: 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.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.