Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Created February 12, 2014 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warmwaffles/8965747 to your computer and use it in GitHub Desktop.
Save warmwaffles/8965747 to your computer and use it in GitHub Desktop.
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