Skip to content

Instantly share code, notes, and snippets.

@dskecse
Created January 18, 2015 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dskecse/700ccf7106c78e3e4c72 to your computer and use it in GitHub Desktop.
Save dskecse/700ccf7106c78e3e4c72 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
ARRAY = (1..10).to_a
Benchmark.ips do |x|
x.report('each') { sum = 0; ARRAY.each { |n| sum += n }; sum }
x.report('reduce') { ARRAY.reduce(0, :+) }
end
Calculating -------------------------------------
each 77.150k i/100ms
reduce 69.590k i/100ms
-------------------------------------------------
each 1.422M (± 5.2%) i/s - 7.098M
reduce 1.198M (± 7.2%) i/s - 5.985M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment