[Bug c++/61621] Normal enum switch slower than test case.
holger.seelig at yahoo dot de
gcc-bugzilla@gcc.gnu.org
Fri Jun 27 12:24:00 GMT 2014
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61621
--- Comment #2 from Holger Seelig <holger.seelig at yahoo dot de> ---
To my understanding and what I know is that a switch like the following:
switch (i)
{
case 10:
func_a();
break;
case 11:
func_b();
break;
case 12:
func_c();
break;
}
Should become something like this:
// N.B.: This is not C++ code
static address jump_table[] = { case_a, case_b, case_c, end };
unsigned int index = i - 10;
if (index > 3) goto end;
goto jump_table [index];
case_a: func_a(); goto end;
case_b: func_b(); goto end;
case_c: func_c();
end:
and with this code a switch should handle many enum cases very efficiently
regardless if a case is executed more frequently. (With a normal enum type the
second line »unsigned int index = i - 10;« could be omitted).
More information about the Gcc-bugs
mailing list