??? 07/06/05 10:17 Read: times |
#96673 - Big switches Responding to: ???'s previous message |
Peter Dannegger said:
"Break big switch statements into nested switches" is also wrong for an 8051. To be fair, the article does specifically say this in the context when "case labels are placed far apart". This is would lead to a "sparse" lookup table, where most of the entries would be unused; eg, switch(x) { case 1: // do stuff break; case 100: // do stuff break; case 200: // do stuff break; case 255: // do stuff break; }A decent modern optimising compiler should probably be smart enough to spot this, and effectively break the the statement up itself, or use an if-else-if cascade. Bigger switches are made with a jump table, so it can be faster since only one comparison with the biggest case value was done. True but, in the example above, the code would be very fast but very big! This just bings us back to the same old story: there is no such thing as "Optimum" or "Best" - it always has to be qualified; eg, "optimum" for speed will often mean sub-optimum for code size; "best" for code size will often mean sub-optimum for speed! :-( Such is life! Remember Red Adair: http://www.8052.com/forum/read.phtml?id=90840 |