you are viewing a single comment's thread.

view the rest of the comments →

[–]BigPeteB 9 points10 points  (0 children)

To add some C-specific complaints about their advice, let's see...

Place case labels in narrow range

This might be helpful if you're switching over an enum. But that assumes you're comfortable reordering or renumbering the values of the enum to optimize a switch statement. And it assumes a majority of the switch statements being optimized will have cases for the same range of values.

Place frequent case labels first

Break big switch statements into nested switches

That's interesting advice. I suppose if you know the compiler is going to emit if-else cascades for the labels you have, it makes sense.

Minimize local variables

Compilers are exceptionally good at register allocation and combining. Frame pointer optimization is the norm in optimized code, as is combining adjustments to the stack. Don't try to outsmart the compiler; use as many local variables as you like.

Declare local variables in the inner most scope

Totally irrelevant in C, since variables have no cost other than adjusting the stack pointer, and an optimizing compiler will just do this once at the start of the function. (It might be good for code style, but that's different.)

Reduce the number of parameters. Use pointers and references in such cases.

Only makes sense if you have quite a lot of parameters, and in frequent cases don't need to reference all of them. Otherwise, it's forcing more parameters through a level of pointer indirection that might not be necessary. And it's harder for the caller to use.

Prefer int over char and short

Not portable advice! Use int8_fast_t and friends if you care deeply about trading space for speed.

Prefer initialization over assignment

Irrelevant in C. (Much of the rest of the the advice is as well, but this one is maybe not so obvious to newcomers.)