Is the use of a PartialFunction
's orElse
more or less efficient than using a large match
block during apply
time?
To illustrate the question, is:
val pf = { case "a" => "A"} orElse
{ case "b" => "B" } orElse
{ case "c" => "C" } ...
more or less efficient than:
val pf = { case "a" => "A"
case "b" => "B"
case "c" => "C"
...
}
during application a value to pf
:
pf(x)
See this detailed analysis from the author of unfiltered. It's basically less efficient. I believe some work has been done in trunk to address this, shortly after the blog post was made.
The second case cannot possibly be less efficient than the first, because the compiler could just convert it into the first (and, in fact, that's not far from what the virtual pattern matcher does).
So, if you have the choice, the second case is always the safer bet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With