public
Description: embedding Lua in Ruby, via FFI
Homepage: http://rufus.rubyforge.org
Clone URL: git://github.com/jmettraux/rufus-lua.git
rufus-lua / test / bm_fiber.rb
100644 79 lines (66 sloc) 1.934 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# benchmarking rufus-lua
#
# Thu Mar 12 15:40:26 JST 2009
#
 
$:.unshift('lib')
 
require 'benchmark'
 
require 'rubygems'
require 'rufus/lua'
 
RUBYFIBS = Fiber.new do
  n1 = n2 = 1
  loop do
    Fiber.yield n1
    n1, n2 = n2, n1+n2
  end
end
#20.times { print RUBYFIBS.resume, ' ' }
 
s = %{
co = coroutine.create(function ()
local n1 = 1; local n2 = 1
while true do
coroutine.yield(n1)
n1, n2 = n2, n1+n2
end
end)
return co
}
 
LUA = Rufus::Lua::State.new
LUAFIBS = LUA.eval(s)
#20.times { print LUAFIBS.resume, ' ' }
 
N = 10_000
Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
  b.report('ruby') do
    N.times { RUBYFIBS.resume }
  end
  b.report('lua via ruby') do
    N.times { LUAFIBS.resume }
  end
  b.report('lua') do
    LUA.eval("for i = 0, #{N} do coroutine.resume(co) end")
  end
end
 
LUA.close
 
 
# $ ruby19 test/bm_fiber.rb
# user system total real
# ruby 0.050000 0.010000 0.060000 ( 0.054605)
# lua via ruby 0.180000 0.000000 0.180000 ( 0.189010)
# lua 0.010000 0.000000 0.010000 ( 0.005543)
#
# $ ruby19 test/bm_fiber.rb
# user system total real
# ruby 0.050000 0.000000 0.050000 ( 0.051531)
# lua via ruby 0.180000 0.010000 0.190000 ( 0.194944)
# lua 0.010000 0.000000 0.010000 ( 0.006325)
#
# $ ruby19 test/bm_fiber.rb
# user system total real
# ruby 0.050000 0.010000 0.060000 ( 0.052032)
# lua via ruby 0.180000 0.000000 0.180000 ( 0.195411)
# lua 0.010000 0.000000 0.010000 ( 0.006394)
#
# $ ruby19 test/bm_fiber.rb
# user system total real
# ruby 0.050000 0.010000 0.060000 ( 0.054892)
# lua via ruby 0.180000 0.000000 0.180000 ( 0.267880)
# lua 0.000000 0.000000 0.000000 ( 0.005865)