简体   繁体   中英

Poor performance of Array.map(f: A => B) in Scala

Please can someone explain to me, why Array.map(f: A=> B) method is implemented in a such way that it is more than 5 times slower than this code:

val list = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
val newList = new Array[Int](size)

var j = 0  
while (j < size) {
  newList(j) = list(j)
  j += 1
}

The method map(f: A=> B) in the Array class, which is provided by TraversableLike trait, uses Scala 'for loop' for iterating over the elements of input Array object, which of course is much slower than using 'while loop'.

Scala version: 2.9.2 Java: jdk1.6.0_23 64bit windows

map is a generic operation (and not specialized (yet)). So you have to box/unbox the operations on the way in and out of the function. Unsurprisingly, it's much slower. This, rather than the style of loop used, is the culprit.

The reason it's done this way is for consistency and ease of maintenance of the code. With infinite numbers of infinitely carefully people working on the code, each method would be hand-crafted for optimal speed while still being generic. Generic utility has been favored over speed, as you can always get speed back by writing the while loop by hand, but if it's not generic and you need it to be, you're stuck.

Improving the performance of Scala with operations on primitive collections is a goal, but probably not the very top goal of the team working on Scala. For now, if you need speed, use the while loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM