Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created October 9, 2013 18:08
Show Gist options
  • Save evanphx/6905570 to your computer and use it in GitHub Desktop.
Save evanphx/6905570 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
require "benchmark"
require 'benchmark/ips'
ARRAY = (1..1_000).to_a
Benchmark.ips do |b|
b.report "each" do
ARRAY.each do |element|
end
end
b.report "for ... in" do
for x in ARRAY
end
end
end
__END__
drax :: ~/scratch> ruby each_benchmark.rb
Calculating -------------------------------------
each 1269 i/100ms
for ... in 1143 i/100ms
-------------------------------------------------
each 14075.7 (±13.4%) i/s - 69795 in 5.064338s
for ... in 12982.5 (±12.0%) i/s - 64008 in 5.016542s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment