Skip to content

Instantly share code, notes, and snippets.

@mcansky
Created June 7, 2012 08:42
Show Gist options
  • Select an option

  • Save mcansky/2887470 to your computer and use it in GitHub Desktop.

Select an option

Save mcansky/2887470 to your computer and use it in GitHub Desktop.
require 'benchmark'
ARRAY = (0..1000).to_a
N = 100000
Benchmark.bmbm do |x|
x.report('each') { N.times {
array = [ ]
ARRAY.each { |c| array << c }
} }
x.report('collect') { N.times {
ARRAY.collect { |e| e}
} }
x.report('inject') { N.times {
ARRAY.inject([]) { |a, e| a << e }
} }
end
# user system total real
# each 1.150000 0.000000 1.150000 ( 1.206118)
# collect 0.970000 0.000000 0.970000 ( 1.041968)
# inject 1.460000 0.010000 1.470000 ( 1.556421)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment