STM revisited: add Scala

Some time ago I compared F# and Haskell STM implementation performance. Today I'm adding Scala into it:

import scala.concurrent.stm._
object Test {
def run(n: Int) = {
val x = Ref(0)
val y = Ref(0)
(1 to n) foreach { i =>
atomic { implicit tx =>
val x1 = x()
val y1 = y()
if (x() != y()) retry
x() = x1 + 1
y() = y1 + 1
}
}
}
def time(block: => Unit) = {
val beginTime=System.currentTimeMillis
block
val endTime=System.currentTimeMillis
println(endTime-beginTime)
}
time(run(1000000))
}
// scala> time(run(1000000))
// 401
view raw stm.scala hosted with ❤ by GitHub

So, it's more than two times slower than Haskell, but more than 3 times faster than F#. Interesting.

Comments

Popular posts from this blog

Regular expressions: Rust vs F# vs Scala

Hash maps: Rust, F#, D, Go, Scala

Haskell: performance