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
目录
相关文章
|
Java API
|
应用服务中间件
LUA 协程 Coroutine
协程 Coroutine 协程(coroutine)并不是 Lua 独有的概念,如果让我用一句话概括,那么大概就是:一种能够在运行途中主动中断,并且能够从中断处恢复运行的特殊函数。(嗯,其实不是函数。
2501 0
|
人工智能 算法 Ruby
|
机器学习/深度学习 人工智能
Lua 协程coroutine
  协程和一般多线程的区别是,一般多线程由系统决定该哪个线程执行,是抢占式的,而协程是由每个线程自己决定自己什么时候不执行,并把执行权主动交给下一个线程。 协程是用户空间线程,操作系统其存在一无所知,所以需要用户自己去做调度,用来执行协作式多任务非常合适。
1467 0