Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Monday, June 4, 2012

Micro Scala benchmark

I wanted to check how well Scala performs doing the same test as in Python on my laptop by zipping 2 identical vectors and calculating the sum. It didn't really surprise me that Scala outperformed Python but the outcome really gives me another WTF moment. I probably made a stupid mistake somewhere but just in case here is the test executed on scala. I did a few tests with smaller vectors and those gave the same results with Python and Scala. The strange outcome is due to the result not fitting into an integer !! Changing generic type [Int] to [Long] will result in ouf-of-memory exception unfortunately. For completeness a link to the simple Profiler used.
import Profiler._

object MicroBenchMark extends App {

  val v1 = 1 to 9999999 //scala includes .to(x) while Python excludes .to(x)
  val v2 = 1 to 9999999
  def vector_op(vector1: Iterable[Int], vector2: Iterable[Int], op: (Int, Int) => Int): Iterable[Int] = {
   profile(println("sum is " +  vector_op(v1, v2, (x,y) => x + y).sum))
   //python prints 99999990000000 instead of 266447232

sum is 266447232
Execution took 8 seconds


  1. Is Profiler your own code? I had to remove this to test the code. It's not really nice to publish code that cannot be copy-pasted and tried straightaway ;)

    Anyway I've got out-of-memory for Int as well. It seems you have different default settings.

  2. Hey Ivan, it's hard to compare our settings but the out-of-memory is due to the autoboxing probably. I added a link to the profiler used for completeness.