zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Scala Ring Benchmark

2023-03-14 10:28:02 时间
  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