[erlang-questions] Idiom for multiple case matches

Michael Radford <>
Fri Nov 7 21:27:42 CET 2008


I hesitate to wade into this cranky-fest, but IMO this is extremely
clear and easy to read:

	is_vowel ($a) -> true;
	is_vowel ($e) -> true;
	is_vowel ($i) -> true;
	is_vowel ($o) -> true;
	is_vowel ($u) -> true;
	is_vowel (_) -> false.

That's just saying directly what you mean in a language that's nice
enough to make it reasonably efficient.   An "ad-hoc micro optimization"
would be something like this, which does actually make a space/time
tradeoff at runtime.

	static char is_vowel_table[256] = { 0, ..., 1, ... 0 };
	#define IS_VOWEL(c) (is_vowel_table[c] != 0)

True, the lists:member solution is less source code, but I think it's a
toss-up in terms of clarity, and I have had far more problems dealing
with code written by "shortest possible source code maniacs" than I have
with "premature optimizers."

I agree that micro benchmarks are irrelevant (except for library
functions).  But when you can have both clarity and efficiency as above,
why not?

Mike

mats cronqvist writes:
> "Hynek Vychodil" <> writes:
> 
> > Anyway, 80% slow down can be good reason to not use lists:member for
> > me when performance is important. I think that when mostly tested
> > characters which are not vowels, it can be more.
> 
> 
>   an "80% slowdown", or indeed any result of any micro-benchmark, is
>   completely and utterly irrelevant. what is (often) important is
>   overall application speed. unfortunately, optimizing a code base
>   full of ad-hoc micro-optimizations can be... challenging.
> 
>   mats
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list