STM revisited: add Scala
Some time ago I compared F# and Haskell STM implementation performance. Today I'm adding Scala into it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
So, it's more than two times slower than Haskell, but more than 3 times faster than F#. Interesting.
Comments