2 ms·
That doesn't seem to have anything to do with the problem. The problem: You have a string. (pointer to char with NUL termination) Depending on what that string
by burfog 8y ago
That doesn't seem to have anything to do with the problem.
The problem: You have a string. (pointer to char with NUL termination) Depending on what that string is, you want to run different code. You want to do this with high performance and a minimum of fuss. No, it isn't an option to say "but what if I used an enum instead?". You have a string. A string is what you have.
Currently in C, you must choose between high performance and a minimum of fuss. Pick one.
The high-performance solution is to use bsearch or a perfect hash, mapping the string to an index of some sort. With purely standard C you would then use a normal switch. Using gcc extensions, you could get slightly better performance with a computed goto. Writing and maintaining this code is a pain, so most people don't bother.
The minimum-fuss solution is a whole bunch of "else if ... else if ... else if ..." that tries each possibility in turn. The performance is terrible, but good enough for stuff like command line option parsing.