Created
February 12, 2014 22:21
-
-
Save warmwaffles/8965747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
max = (ARGV.shift || 10_000_000).to_i | |
puts "# of iterations = #{max}" | |
Benchmark::bm(20) do |x| | |
x.report("each") do | |
(0..max).each do | |
end | |
end | |
x.report("for") do | |
for i in 0..max do | |
# do nothing | |
end | |
end | |
x.report("each-var") do | |
(0..max).each do |i| | |
end | |
end | |
x.report("while") do | |
n = 0 | |
while true do | |
break if n >= max | |
n += 1 | |
end | |
end | |
x.report("loop") do | |
n = 0 | |
loop do | |
break if n >= max | |
n += 1 | |
end | |
end | |
x.report("begin") do | |
n = 0 | |
begin | |
n += 1 | |
end while(n < max) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment