Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created July 13, 2012 16:44
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 Mon-Ouie/3105906 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/3105906 to your computer and use it in GitHub Desktop.
require 'benchmark'
N = 1_000
ary = (1..10_000).to_a
Benchmark.bm 15 do |x|
x.report("block") do
N.times { ary.map { |n| n * 2 } }
end
x.report("with object") do
N.times { ary.each_with_object(2).map(&:*) }
end
x.report("method") do
N.times { ary.map(&2.method(:*)) }
end
end
__END__
user system total real
block 0.760000 0.000000 0.760000 ( 0.771719)
with object 1.040000 0.000000 1.040000 ( 1.040319)
method 1.290000 0.000000 1.290000 ( 1.295140)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment