Skip to content

Instantly share code, notes, and snippets.

@Mikoangelo
Created August 7, 2008 13:36
Show Gist options
  • Save Mikoangelo/4401 to your computer and use it in GitHub Desktop.
Save Mikoangelo/4401 to your computer and use it in GitHub Desktop.
require "benchmark"
TESTS = 1_000_000
array = [1,2,3]
Benchmark.bmbm do |results|
results.report " for" do
TESTS.times do
for a in array
"foo"
end
end
end
results.report "each" do
TESTS.times do
array.each do |a|
"foo"
end
end
end
end
Rehearsal ----------------------------------------
for 2.490000 0.030000 2.520000 ( 2.947099)
each 2.630000 0.030000 2.660000 ( 3.169486)
------------------------------- total: 5.180000sec
user system total real
for 2.510000 0.030000 2.540000 ( 3.117419)
each 2.630000 0.040000 2.670000 ( 3.330009)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment