If Migrated
is a unchanging list that really is as short as you show then you should just hard-code the strings into the code (or use a macro, or declare them const
(make sure they're const pointers as well as pointers to const)). That way the compiler can optimise the strcmp
calls away to almost nothing; short strings like you show might even be done via a numeric comparison (a four byte string is the same length as a number), but don't do it manually - let the compiler sort that out. The key point is that the compiler cannot optimize this to the maximum if it can't prove that Migrated
is constant.
If Migrated
is unchanging, but much larger than shown, then you should use "perfect hashing". There is a tool called gperf
that, given a list of names, will generate C code to do the lookup in an optimal way.
If Migrated
can change at run-time and is small then you're probably just about optimal already.
If Migrated
can change at run-time, but can be very large then a hash table is the best option. Failing that a sorted, balanced tree would be better than a list.
Migrated
array they yes, it is optimized, but if there are thousands of items then there are better ways to code it. \$\endgroup\$codeB
and based on it compare the string with only one viable item in the array. \$\endgroup\$