Created
October 9, 2013 18:08
-
-
Save evanphx/6905570 to your computer and use it in GitHub Desktop.
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" | |
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