bool isvowel(char c){ return (1<<c-'a')&0x104111; }
Often O(1) is better than O(n) even for small n.Often O(1) is better than O(1).
Modern compilers will turn
bool isvowel(char c){
switch(c){
case'a':case'e':case'i':case'o':case'u':
return true;
}
return false;
}
into sort-of the same code (after a range check).
https://godbolt.org/z/aMcdY6Looking at the code actually produced, the compiler has noticed that all the vowels are even-numbered characters (0,4,8,14,20), so combined the range check with a "rotate-right" so it can compress 0x104111 down to 0x495, and shift that right and check the low bit of the result instead of doing a "bt", or bit-test. It's anybody's guess why that is considered better; shifts are supposed to be constant-time; but checking the low bit is a byte-sized operation. So, maybe
bool isvowel(char c){ return (0x104111>>c-'a')&1; }
is better: https://godbolt.org/z/M784cK