Last active
August 29, 2015 14:05
-
-
Save britishtea/e96ca4be5a0a841206d3 to your computer and use it in GitHub Desktop.
Benchmark: #each vs for
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
require "benchmark" | |
require "benchmark/ips" | |
range = Array(1..100_000) | |
Benchmark.ips do |x| | |
x.report("each") { range.each { |x| } } | |
x.report("for") { for x in range; end } | |
end | |
# $ ruby -v | |
# ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0] | |
# $ ruby for.rb | |
# Calculating ------------------------------------- | |
# each 10 i/100ms | |
# for 9 i/100ms | |
# ------------------------------------------------- | |
# each 107.3 (±2.8%) i/s - 540 in 5.034679s | |
# for 97.5 (±5.1%) i/s - 486 in 4.999733s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment