Lua、LuaJIT Coroutine和Ruby Fiber的切换效率对比

最近重读了《Programming Lua》,对协程做了重点复习。众所周知,Ruby1.9引入了Fiber,同样是coroutine,不过Ruby Fiber支持全对称协程(通过fiber库),而Lua只支持所谓半对称协程。

    这里将对Lua、LuaJIT和Ruby Fiber的切换效率做个对比测试,测试场景很简单:两个coroutine相互切换达到5000万次,统计每秒切换的次数,各测试多次取最佳。

    lua的程序如下:
   
c1 = coroutine . create(function ()
                     
while  true  do
                       coroutine
. yield()
                     end
                    end)

c2
= coroutine . create(function ()
                     
while  true  do
                       coroutine
. yield()
                     end
                    end)

local  start = os . clock()
local  count = 50000000

for  i = 1 , count  do
 coroutine
. resume(c1)
 coroutine
. resume(c2)
end

print ( 4 * count / (os . clock() - start))

    考虑到在循环中事实上发生了四次切换:main->c1,c1->main,main->c2,c2->main,因此乘以4。

    Ruby Fiber的测试分两种,采用transfer的例程如下:

require   ' fiber '
require   ' benchmark '

Benchmark
. bm  do   | x |
  MAX_COUNT
= 50000000
  f1
= Fiber . new  do   | other , count |
     
while  count < MAX_COUNT
      other
, count = other . transfer(Fiber . current , count . succ)
     end
  end

  f2
= Fiber . new  do   | other , count |
    
while  count < MAX_COUNT
      other
, count = other . transfer(Fiber . current , count . succ)
    end
  end

  x
. report{
    f1
. resume(f2 , 0 )
  }
end
     Ruby Fiber采用resume/yield的例程如下:
require   ' benchmark '
f1
= Fiber . new  do
  
while  true
    Fiber
. yield
  end
end
f2
= Fiber . new  do
  
while  true
    Fiber
. yield
  end
end

COUNT
= 50000000

Benchmark
. bm  do   | x |
  x
. report{
     COUNT
. times   do
         f1
. resume
         f2
. resume
     end
  }
end



     测试环境:
          CPU :    Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz
          Memory:  3GB
          System :  Linux dennis-laptop 2.6.31-14-generic #48-Ubuntu SMP
          Lua    : 5.1.4
          ruby  :  1.9.1p378
          LuaJIT:  1.1.5和2.0.0-beta2

      测试结果如下:
    
  Lua LuaJIT 1.1.5
 LuaJIT 2.0.0-beta2
 ruby-transfer
 ruby-resume/yield
 次数 6123698 9354536 24875620 825491 969649


      结论:
      1、lua的协程切换效率都是百万级别,luaJIT 2.0的性能更是牛叉,切换效率是原生lua的4倍,达到千万级别。
      2、相形之下,Ruby Fiber的效率比较差了,十万级别。
      3、Ruby使用transfer的效率比之resume/yield略差那么一点,排除一些测试误差,两者应该是差不多的,从ruby源码上看resume/yield和transfer的调用是一样的,resume还多了几条指令。
      4、额外信息,Ruby Fiber和lua coroutine都只能跑在一个cpu上,这个测试肯定能跑满一个cpu,内存占用上,lua也比ruby小很多。
 文章转自庄周梦蝶  ,原文发布时间2010-03-02
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值