Scala实现的ring benchmark:
import
scala.actors.Actor
import scala.actors.Actor._ import java.util.concurrent.CountDownLatch case class Message() class Process(m:Int,p:Actor,latch:CountDownLatch) extends Actor{ var next = p def act{ loop{ recvAndSend(m) } } def recvAndSend(count:Int){ if (count == 0 ){ latch.countDown() exit } else { react{ case Message() => next ! Message() recvAndSend(count - 1 ) } } } } object RingBenchmark{ def main(args:Array[String]){ start(args( 0 ).toInt,args( 1 ).toInt) } def start(n:Int,m:Int){ val latch = new CountDownLatch(n) val first = new Process(m, null ,latch) val p = createProcess(first,n - 1 ,m,latch) first.next = p val start:Long = System.currentTimeMillis first.start first ! Message() latch.await() println(System.currentTimeMillis - start) } def createProcess(p:Actor,n:Int,m:Int,latch:CountDownLatch):Actor = { if (n == 0 ) p else { val next = new Process(m,p,latch) next.start createProcess(next,n - 1 ,m,latch) } } } 与Erlang版本的比较(单位毫秒),scala版本2.7.4-final,erlang是R13B, windows xp
|