Efficiency PartialFunction orElse

Is using PartialFunction orElse more or less efficient than using a large match block for apply time?

To illustrate the question, follow these steps:

 val pf = { case "a" => "A"} orElse { case "b" => "B" } orElse { case "c" => "C" } ... 

more or less effective than:

 val pf = { case "a" => "A" case "b" => "B" case "c" => "C" ... } 

during application pf value:

 pf(x) 
+6
source share
2 answers

Detailed analysis from the author unfiltered . It is basically less effective. I believe that some work was done in the trunk to solve this problem, shortly after a blog entry was made.

+5
source

The second case cannot be less efficient than the first, because the compiler can simply convert it to the first (and, in fact, one that is not so close to what the virtual template pairing does).

So, if you have a choice, the second case is a safer bet.

+1
source

Source: https://habr.com/ru/post/909520/


All Articles