Created
March 14, 2022 10:27
-
-
Save cooljl31/bcd34ea1d6d61b88e7839e3346855b11 to your computer and use it in GitHub Desktop.
each_benchmark.rb
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
#!/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