以前、
http://qiita.com/Nabetani/items/9776d99d09476d07c9e2
と
http://qiita.com/Nabetani/items/47d357cc7f80f5c42f0d
でベンチマークの記事を書いたんだけど、2.2.3 とか jruby9000 が出ているのでまた勝手なベンチマークをとってみた。
当初ここに書いた記事では JRuby が Java7 になっていたので、Java8で測りなおして追記した。
「3倍以上」より「4倍以上」が適切なようなので、タイトルも変えてみた。
まずはグラフ。
横軸は時間なので、長いほうが遅い。
simple_lambda の方は、jruby がやけに頑張っている。
jじゃないrubyでは、新しくなるほど速くなっていて、開発者の皆様ありがとうございますという感じ。
対照的に、complex_string では jruby が遅い。
jじゃないrubyでも、誤差なのか本当なのか、2.2.3 が一番遅い。
測定に使ったのはこんなコード
#encode:utf-8
(p RUBY_DESCRIPTION) rescue p "1.8.x or former"
require "benchmark"
def complex_string(size)
a=""
size.times do
s='"#''{!'*1000+"1"+'.to_s}"'*1000
a+=eval(s)
end
end
def simple_lambda(size)
a=->(b){->(c){->(d){->(e){ (->(f){f+1})[e] }[d] }[c] }[b] }
x=0
(size*100).times do
x=a[a[a[a[a[a[a[x]]]]]]]
end
end
def run_bench( size )
puts "size : #{size}"
m=[ :complex_string, :simple_lambda ]
len = m.map(&:to_s).map(&:size).max
Benchmark.bmbm(len) do |x|
m.each do |s|
x.report(s.to_s){
self.send(s, size)
}
end
end
end
run_bench( 200 )
結果のテキストも面白いのでそのまま貼ってみる。
"ruby 2.0.0p647 (2015-08-18 revision 51631) [x86_64-darwin14.5.0]"
size : 200
Rehearsal --------------------------------------------------
complex_string 0.450000 0.000000 0.450000 ( 0.450806)
simple_lambda 0.440000 0.010000 0.450000 ( 0.447935)
----------------------------------------- total: 0.900000sec
user system total real
complex_string 0.420000 0.000000 0.420000 ( 0.418569)
simple_lambda 0.460000 0.020000 0.480000 ( 0.479465)
"ruby 2.1.7p400 (2015-08-18 revision 51632) [x86_64-darwin14.0]"
size : 200
Rehearsal --------------------------------------------------
complex_string 0.400000 0.010000 0.410000 ( 0.411405)
simple_lambda 0.470000 0.010000 0.480000 ( 0.479922)
----------------------------------------- total: 0.890000sec
user system total real
complex_string 0.390000 0.010000 0.400000 ( 0.404211)
simple_lambda 0.470000 0.000000 0.470000 ( 0.463658)
"ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]"
size : 200
Rehearsal --------------------------------------------------
complex_string 0.440000 0.010000 0.450000 ( 0.446467)
simple_lambda 0.330000 0.000000 0.330000 ( 0.335376)
----------------------------------------- total: 0.780000sec
user system total real
complex_string 0.450000 0.000000 0.450000 ( 0.443004)
simple_lambda 0.350000 0.010000 0.360000 ( 0.354291)
"jruby 9.0.0.0 (2.2.2) 2015-07-21 e10ec96 Java HotSpot(TM) 64-Bit
Server VM 23.3-b01 on 1.7.0_07-b10 +jit [darwin-x86_64]"
size : 200
Rehearsal --------------------------------------------------
complex_string 1.780000 0.020000 1.800000 ( 1.110261)
simple_lambda 0.470000 0.010000 0.480000 ( 0.275905)
----------------------------------------- total: 2.280000sec
user system total real
complex_string 0.860000 0.010000 0.870000 ( 0.790705)
simple_lambda 0.100000 0.000000 0.100000 ( 0.093053)
"jruby 9.0.0.0 (2.2.2) 2015-07-21 e10ec96 Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [darwin-x86_64]"
size : 200
Rehearsal --------------------------------------------------
complex_string 1.670000 0.050000 1.720000 ( 0.746955)
simple_lambda 0.510000 0.020000 0.530000 ( 0.164587)
----------------------------------------- total: 2.250000sec
user system total real
complex_string 0.620000 0.010000 0.630000 ( 0.543345)
simple_lambda 0.080000 0.000000 0.080000 ( 0.071758)
なんといっても面白いのは、 jruby。リハーサルと本番の速度差がすごい。
結論はいつもと同じで、
- 開発者のみなさまありがとうございます。
- JRuby と CRuby。どっちが速いかは、動かすコードによる。