Permalink
Browse files

add benchmark comparing block.call v yield

[ci skip]
  • Loading branch information...
1 parent 268eeb4 commit 5436bc2b46dc889937c64cd33990c0133c49c23f @dchelimsky dchelimsky committed Mar 20, 2012
Showing with 81 additions and 0 deletions.
  1. +81 −0 benchmarks/call_v_yield.rb
@@ -0,0 +1,81 @@
+require 'benchmark'
+
+n = 100_000
+
+def call_block(&block)
+ block.call
+end
+
+def yield_control
+ yield
+end
+
+Benchmark.benchmark do |bm|
+ puts "#{n} times - ruby #{RUBY_VERSION}"
+
+ puts
+ puts "eval"
+
+ 3.times do
+ bm.report do
+ n.times do
+ eval("2 + 3")
+ end
+ end
+ end
+
+ puts
+ puts "call block"
+
+ 3.times do
+ bm.report do
+ n.times do
+ call_block { 2 + 3 }
+ end
+ end
+ end
+
+ puts
+ puts "yield"
+
+ 3.times do
+ bm.report do
+ n.times do
+ yield_control { 2 + 3 }
+ end
+ end
+ end
+
+ puts
+ puts "exec"
+
+ 3.times do
+ bm.report do
+ n.times do
+ 2 + 3
+ end
+ end
+ end
+end
+
+# 100000 times - ruby 1.9.3
+#
+# eval
+# 0.870000 0.010000 0.880000 ( 0.877762)
+# 0.890000 0.000000 0.890000 ( 0.891142)
+# 0.890000 0.000000 0.890000 ( 0.896365)
+#
+# call block
+# 0.120000 0.010000 0.130000 ( 0.136322)
+# 0.130000 0.010000 0.140000 ( 0.138608)
+# 0.130000 0.000000 0.130000 ( 0.129931)
+#
+# yield
+# 0.020000 0.000000 0.020000 ( 0.020412)
+# 0.010000 0.000000 0.010000 ( 0.017926)
+# 0.020000 0.000000 0.020000 ( 0.025740)
+#
+# exec
+# 0.010000 0.000000 0.010000 ( 0.009935)
+# 0.010000 0.000000 0.010000 ( 0.011588)
+# 0.010000 0.000000 0.010000 ( 0.010613)

0 comments on commit 5436bc2

Please sign in to comment.