庄周梦蝶

生活、程序、未来
   :: 首页 ::  ::  :: 聚合  :: 管理

Scala Ring Benchmark

Posted on 2009-06-09 13:42 dennis 阅读(1590) 评论(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



评论

# re: Scala Ring Benchmark  回复  更多评论   

2009-06-13 09:40 by metadmin
语法有些类C的味道。代码没有比JAVA简化多少。 RUBY简化的很多。

---------------------------------
解开权限与业务耦合,提高开发效率
细粒度权限管理软件 试用版下载
http://www.metadmin.com

# re: Scala Ring Benchmark  回复  更多评论   

2009-06-13 10:31 by dennis
@metadmin
ruby写的ring benchmark ,代码不少的。

# re: Scala Ring Benchmark  回复  更多评论   

2009-11-09 18:02 by arbow
$ scala RingBenchmark 1000 10000
24332
$ scala -Dactors.corePoolSize=1 RingBenchmark 1000 10000
16870
$ scala -Dactors.corePoolSize=2 RingBenchmark 1000 10000
20646

调度器线程数的影响还是比较明显

只有注册用户登录后才能发表评论。


网站导航: