mardi 4 novembre 2014

In Scala, is there any performance difference between List() and Nil?


Vote count:

0




Suppose I have a function that accepts a object and a list:



case class Point(x: Int, y: Int)
def f1(w: Point, l: List[String]) = { /* do something /* }


I would typically use it like this:



val w = Point(1,1)
val lst = List("Hello", "world")
f1(w, lst) // non empty list


Many times I would need to call the function with empty list as second parameter:



f1(w, List()) // empty list
f1(w, Nil) // empty list


Is there any performance difference between the last two lines ?


I think using List() will invoke List.apply() method. Does Scala compiler optimize it to Nil?



asked 1 min ago

tuxdna

2,089






In Scala, is there any performance difference between List() and Nil?

Aucun commentaire:

Enregistrer un commentaire