Skip to content

Instantly share code, notes, and snippets.

@raggi
Created December 3, 2009 12:17
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 raggi/248110 to your computer and use it in GitHub Desktop.
Save raggi/248110 to your computer and use it in GitHub Desktop.
closures aren't cheap, srsly
require "benchmark"
arr = [1,2,3,4,5,6,7,8,9,0] * 100
GC.start
TESTS = 10_000
Benchmark.bmbm do |results|
results.report("map:") { TESTS.times { arr.map { |i| i } } }
results.report("inj:") { TESTS.times { a = []; arr.inject(a, :<<) } }
end
# Rehearsal ----------------------------------------
# map:   1.790000   0.020000   1.810000 (  2.070154)
# inj:   1.700000   0.010000   1.710000 (  1.854312)
# ------------------------------- total: 3.520000sec
#
#            user     system      total        real
# map:   1.780000   0.010000   1.790000 (  1.839637)
# inj:   1.700000   0.010000   1.710000 (  1.752435)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment