Created
February 3, 2012 16:38
-
-
Save marianposaceanu/1731012 to your computer and use it in GitHub Desktop.
let_the_benchmarks_begin.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
# For vs. each | |
# Results: | |
# | |
# user system total real | |
# 0.340000 0.000000 0.340000 ( 0.341422) | |
# 0.310000 0.000000 0.310000 ( 0.326168) | |
require 'benchmark' | |
n = 500000 | |
Benchmark.bm do |x| | |
x.report do | |
foo = nil | |
bar = [1,2,3,4,5] | |
n.times do | |
for i in bar do | |
foo = i | |
end | |
end | |
end | |
x.report do | |
foo = nil | |
bar = [1,2,3,4,5] | |
n.times do | |
bar.each do |i| | |
foo = i | |
end | |
end | |
end | |
end |
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
# Obvious if you think about it | |
# but hey it needed to be benched | |
# Results: | |
# | |
# user system total real | |
# 0.110000 0.000000 0.110000 ( 0.105050) | |
# 0.470000 0.020000 0.490000 ( 0.493622) | |
require 'benchmark' | |
n = 500000 | |
Benchmark.bm do |x| | |
x.report do | |
something = {} | |
n.times do | |
something[:else] = 2 | |
end | |
end | |
x.report do | |
n.times do | |
something ||= {} | |
something[:else] = 2 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment