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
    评论
Lua 中,协程(coroutine)是一种特殊的线程,可以在执行过程中挂起和恢复。协程可以用于实现协作式多任务,允许程序在某个点上暂停,并在稍后的时间点继续执行。下面是一些常见的 Lua 协程使用方法: 1. 创建协程:使用 `coroutine.create` 函数来创建一个新的协程。 ```lua local co = coroutine.create(function() -- 协程的执行逻辑 end) ``` 2. 启动协程:使用 `coroutine.resume` 函数来启动或恢复一个协程的执行。 ```lua local success, result = coroutine.resume(co) ``` 3. 挂起协程:使用 `coroutine.yield` 函数来手动挂起一个协程的执行,并返回结果。 ```lua local function foo() print("Hello") coroutine.yield() print("World") end local co = coroutine.create(foo) coroutine.resume(co) -- 输出 "Hello" coroutine.resume(co) -- 输出 "World" ``` 4. 检查协程状态:使用 `coroutine.status` 函数来检查协程的状态。 ```lua local status = coroutine.status(co) ``` 常见的状态有: - "suspended":协程处于挂起状态,即已经创建但尚未执行或已经执行但被挂起。 - "running":当前正在执行的协程。 - "dead":协程已经执行完毕或发生错误导致终止。 以上是一些基本的协程使用方法,还可以使用 `coroutine.wrap` 函数将一个函数转换为协程。在实际应用中,协程可以用于实现状态机、异步操作、迭代器等功能。根据具体需求,可以灵活运用协程的特性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值