I’ve been reading up on all the great new stuff in Scala 2.10.0-RC3, and as always, one of the features mentioned is performance improvements. I was curious today what that meant for functional style work, so I wrote up a short microbenchmark to test it. I created a Sieve of Eratosthenes using streams, and a Sieve of Sundaram, both shown below:

Sieve of Eratosthenes –

def eratosthenes(toNum: Int) = { def sieveHelp(r: IndexedSeq[Int]): Stream[Int] = { if(r.isEmpty) Stream.empty else r.head #:: sieveHelp(r.tail.filterNot(_ % r.head == 0)) } sieveHelp(2 +: (3 to toNum by 2)) }

Sieve of Sundaram –

def sundaram(toNum: Int) = { val n = (toNum - 2)/2 val nonPrimes = for (i <- 1 to n; j <- i to (n - i) / (2 * i + 1)) yield i+j+(2*i*j) 2 +:((1 to n) diff nonPrimes map (2*_+1)) }

The Sieve of Sundaram is run 120 times, finding all primes below 3,000,000. The Sieve of Eratosthenes is run 60 times and finds all primes below 75,000. The results for both are in the chart below:

As you can see, the performance improvement for Sundaram are negligible, but the the performance of the Sieve of Eratosthenes is more than doubled! All in all, I’m looking forward to the release of 2.10 and further upgrades to the speed of the fastest and most flexible non-java jvm language.

For the source of my benchmark, go here: https://github.com/markehammons/2.10.0-RC3-Benchmark