Talk:Optimizing C++/Code optimization/Instruction count


 * "As further optimization, you can count, in typical runs, the actual number of times every case is chosen, and sort the cases from the most frequent to the less frequent."

I'd like to see benchmark figures for this (or for any other claim in this book, for that matter). The whole idea of a  is that it can be compiled into a "computed goto" that uses the switch expression to determine how far to jump. The fastest switches are those for which all the cases are contiguous integers, i.e. without gaps as in:

... because in this case, the compiler needs to generate a check that. Qwertyus (discuss • contribs) 14:19, 3 June 2014 (UTC)

No, it doesn't need to check it, it just puts a goto to the closing } of the switch on the case of the number 2.

Also, as he stated before, this is very important in cases where a table is not used.

As the compiler will not create a table with 3456 entries for this, you're generating:

And if you call that all the time with i==3, that should be the first comparison, not the i == 101. --186.182.199.172 (discuss) 07:46, 17 February 2015 (UTC)