Re: When to use if-if, else-if, switch, goto and local scope vars?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



lynx.abraxas@xxxxxxxxxx writes:

> - when to use switch instead of else-if statments
> - how many cases there have to be for switch to be better than else-if?
> - is an if-if statement better  than  if-else-if  or  switch  when  it  is  an
> exclusive decission?
> - is it better to nest switch statement or use else-if instead of nesting?
> - does it save RAM when I define variables in the innermost scope possible?
> - when does it help writing out loops?
> - should I concider using banned goto instructions for microcontrollers?

For these sorts of micro-optimizations, the compiler will make
decisions that are as good as yours, if not better.  It's generally
preferable to write the code so that it is readable, and let the
compiler work out what to do.

The places where you can help the compiler are by writing faster
general algorithms.  At the micro level, you often have a better idea
of which pointers might point to the same areas of memory than the
compiler does; judicious use of the restrict qualifier can help some
types of code.  Also, if the compiler can not see a function you are
calling (because it is defined in a different source file), it has to
call the function pretty much as you specify, so make sure you don't
call it unnecessarily.

> Reuse is best sure, but what about:
> 
> void Openfile(void){
> if (error){
>         char s[5];
>         itoa(int, s, 10);
>         lcd_write_str(s);
>         }
> }
> 
> Is  the  char-array  only  allocated  when  the  error  occures, or will it be
> allocated when the function is called, or is it  already  allocated  when  the
> whole programme is loaded?

The array is allocated on the stack at runtime when the function is
called.

> Is there a method to let the preprocessor unroll loops?

See the -funroll-loops option.  In general you should read the
compiler manual to see the sorts of optimization options you can
specify.

Ian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux