Skip to content

Instantly share code, notes, and snippets.

@cooljl31
Created March 14, 2022 10:27
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 cooljl31/bcd34ea1d6d61b88e7839e3346855b11 to your computer and use it in GitHub Desktop.
Save cooljl31/bcd34ea1d6d61b88e7839e3346855b11 to your computer and use it in GitHub Desktop.
each_benchmark.rb
#!/usr/bin/env ruby -w
require "benchmark"
TIMES = 100_000
ARRAY = (1..1_000).to_a
Benchmark.bm(30) do |b|
b.report "each" do
TIMES.times do |i|
ARRAY.each do |element|
end
end
end
b.report "for ... in" do
TIMES.times do |i|
for x in ARRAY
end
end
end
end
__END__
user system total real
each 9.710000 0.010000 9.720000 ( 9.740620)
for ... in 7.460000 0.000000 7.460000 ( 7.475158)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment