最近重读了《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
'
Ruby Fiber采用resume/yield的例程如下:
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
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 测试结果如下:
结论: 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 |