Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created December 14, 2009 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tenderlove/256393 to your computer and use it in GitHub Desktop.
Save tenderlove/256393 to your computer and use it in GitHub Desktop.
require 'benchmark'
def each_acc list
foos = []
list.each do |letter|
foos << letter.to_i
end
foos
end
def inject_acc list
list.inject([]) do |memo, item|
memo << item.to_i
end
end
n = 50000
LIST = ('a'..'z').to_a
Benchmark.bm(7) do |x|
x.report('each') do
n.times { each_acc(LIST) }
end
x.report('inject') do
n.times { inject_acc(LIST) }
end
end
user system total real
each 0.810000 0.000000 0.810000 ( 0.823612)
inject 1.600000 0.010000 1.610000 ( 1.608838)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment