Posted on 2009-06-09 13:42
dennis 阅读(1599)
评论(3) 编辑 收藏 所属分类:
动态语言 、
java
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
N |
M |
Scala |
Erlang |
1000 |
100 |
297 |
62 |
1000 |
500 |
1328 |
343 |
1000 |
1000 |
2469 |
671 |
10000 |
100 |
2812 |
781 |
10000 |
1000 |
28796 |
7797 |