Last active
August 29, 2015 14:05
-
-
Save cameron-martin/b907ec43a9d8b9303bdc 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
require 'benchmark' | |
require 'ostruct' | |
n = 10_000 | |
array = Array.new(1000) { |index| OpenStruct.new.tap { |struct| struct.price = index } }; | |
Benchmark.bmbm do |x| | |
x.report('two stages') { n.times { array.map(&:price).inject(0, :+) } } | |
x.report('one stage') { n.times { array.inject(0) { |sum, product| sum += product.price } } } | |
x.report('lazy') { n.times { array.lazy.map(&:price).inject(0, :+) } } | |
end; |
Author
cameron-martin
commented
Aug 19, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment