Comments by "MrAbrazildo" (@MrAbrazildo) on "why are switch statements so HECKIN fast?" video.
-
0:00, "....Nice day for fishing, ain't it?"
I made several tests of switch (different versions) vs if-block. Switches w/o default cases, w/o a case for numbers < and w/o number > than some value, and so on. I found that switches have a more unstable performance, some faster, others slower.
Array use to be faster than switch, by ~15% according to my tests. I remember only 1 combination of switch beating 1 array alg. So, it's better to rewrite this as:
const auto Options = "qcned";
const auto ptr = strchr (Options, optionbuf[0]);
if (!ptr) break; //Default case of switch.
#define _(Task) handle##Task
const auto funcs[] = { _(Stop), _(Continue), _(New), _(Edit), _(Delete) };
(funcs[ptr - Options]) ( ); //Cases are solved on the indexing. Function called right away.
1