Hi All
Consult a while, has any tips can speed the judgment of"Switch case" in keil c??
Seem to make the common use parameter putting the front to judge first ??
For examples,if c equal one more then equal two
so we can write down like this
switch(c) { case 1: break; case 2: break; }
an we don't use this type
switch(c) { case 2 break; case 1 break; }
Is any tips like this can use in "switch case" in keil c??
Sorry for my poor english^^"""
Thanks
Is any other tips can help "switch case" speed up??
If you want full control, you will need to write your own implementation in assembly.
You cannot assume that the compiler will pick a particular way to implement a switch/case every time. It depends on the optimization level, your code, the version of the compiler, etc.
There are different methods the compiler can use to compile a switch/case statement.</i.
Yes, but it does not.
For some elusive reason (In my opinion - to make the optimizer more impressive, nothing else), the compiler always does it the slow way and, although the compiler would be perfectly capable of doing so, any change to a faster implementation is handled by the optimizer, which, at level 8 will speed up some switch/case statements.
Erik
From other threads, I recall that the order of switch labels has an effect on the implementation. The jump table is more likely to be used when all the labels are in order.
So, changing from 1 - 2 to 2 - 1 isn't necessarily a comparison of an if-then-else implementation in two different orders. You may be comparing an if-then chain with a jump table. Check the assembler output to be sure.
If it really matters to you, you can always write your own if-then-else chain to be certain. Occasionally it is worth improving one path at the expense of others. More often, even when the difference matters, the time actually executing the switch isn't really a dominant factor.
Thanks all
I just want to know
1 how is ccase realy do??
2 how can we write the correct code to help complier to output great code
3 how to write my own if-then-else chain or others ?? is any example code??
for now,it seem look like the parameter of switch
is much better in order and closed
CODE1
#define AA 0 #define BB 1 #define CC 2 ~ #define ZZ 26 switch(c) { case AA: break; ~ case ZZ: break; }
CODE2
#define AA 0 #define BB 10 #define CC 20 ~ #define ZZ 255 switch(c) { case DD: break; ~ case GG: break; case MM: break; }
if the value of c is form AA to ZZ ,The code1 speed is faster then the code2 in optimizer level 7
thanks a again