Is scala functional programming slower than traditional coding?

Posted by Fred Haslam on Stack Overflow See other posts from Stack Overflow or by Fred Haslam
Published on 2010-05-08T16:42:24Z Indexed on 2010/05/08 16:48 UTC
Read the original article Hit count: 194

In one of my first attempts to create functional code, I ran into a performance issue.

I started with a common task - multiply the elements of two arrays and sum up the results:

var first:Array[Float] ...
var second:Array[Float] ...    
var sum=0f; 
for(ix<-0 until first.length) sum += first(ix) * second(ix);

Here is how I reformed the work:

sum = first.zip(second).map{ case (a,b) => a*b }.reduceLeft(_+_)

When I benchmarked the two approaches, the second method takes 40 times as long to complete!

Why does the second method take so much longer? How can I reform the work to be both speed efficient and use functional programming style?

© Stack Overflow or respective owner

Related posts about scala

Related posts about functional-programming