Scala Ring Benchmark

    xiaoxiao2024-06-24  109

      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 文章转自庄周梦蝶  ,原文发布时间2009-06-09 相关资源:敏捷开发V1.0.pptx
    最新回复(0)