Skip to content

Instantly share code, notes, and snippets.

@nagachika
Created April 22, 2011 03:12
Show Gist options
  • Save nagachika/935934 to your computer and use it in GitHub Desktop.
Save nagachika/935934 to your computer and use it in GitHub Desktop.
benchmark with/without return
require "benchmark"
def without_return
0
end
def with_return
return 0
end
def with_block(&blk)
blk.call
end
def with_block_without_return
with_block { 0 }
end
def with_block_with_return
with_block { return 0 }
end
num = 10000000
Benchmark.bm do |x|
x.report("without_return") do
i = 0
while i < num
without_return
i += 1
end
end
x.report("with_return") do
i = 0
while i < num
with_return
i += 1
end
end
x.report("with_block_without_return") do
i = 0
while i < num
with_block_without_return
i += 1
end
end
x.report("with_block_with_return") do
i = 0
while i < num
with_block_with_return
i += 1
end
end
end
% ./ruby-trunk -v
ruby 1.9.3dev (2011-04-21 trunk 31316) [i686-linux]
% ./ruby-trunk bm_return.rb
user system total real
without_return 5.850000 0.000000 5.850000 ( 5.905988)
with_return 5.790000 0.000000 5.790000 ( 5.810832)
with_block_without_return 38.080000 0.140000 38.220000 ( 38.525464)
with_block_with_return 57.610000 0.340000 57.950000 ( 58.209715)
ruby 1.9.2p94 (2010-12-08 revision 31204) [i686-linux]
user system total real
without_return 0.870000 0.000000 0.870000 ( 0.874565)
with_return 0.850000 0.000000 0.850000 ( 0.849621)
with_block_without_return 5.220000 0.000000 5.220000 ( 5.219810)
with_block_with_return 9.900000 0.000000 9.900000 ( 9.906881)
ruby 1.9.3dev (2011-04-23 trunk 31323) [x86_64-darwin10.7.0]
user system total real
without_return 0.820000 0.000000 0.820000 ( 0.818726)
with_return 0.830000 0.000000 0.830000 ( 0.825238)
with_block_without_return 13.280000 0.030000 13.310000 ( 13.291957)
with_block_with_return 15.630000 0.040000 15.670000 ( 15.831282)
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10.3.0]
user system total real
without_return 5.450000 0.000000 5.450000 ( 5.453376)
with_return 6.440000 0.000000 6.440000 ( 6.436711)
with_block_without_return 35.040000 1.600000 36.640000 ( 36.632049)
with_block_with_return 37.950000 1.640000 39.590000 ( 39.597771)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment