Skip to content

Instantly share code, notes, and snippets.

@raggi
Created August 5, 2009 18:41
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/162877 to your computer and use it in GitHub Desktop.
Save raggi/162877 to your computer and use it in GitHub Desktop.
require "benchmark"
KEY = '1234567890'
HASH = {
KEY => KEY
}
TESTS = 2_000_000
desc = defined?(RUBY_DESCRIPTION) ? RUBY_DESCRIPTION : "#{RUBY_ENGINE} #{JRUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_VERSION}-#{RUBY_PATCHLEVEL} #{RUBY_RELEASE_DATE}"
puts "#{TESTS} iterations on #{desc}"
Benchmark.bmbm do |results|
results.report("null:") { TESTS.times { } }
results.report("literal:") { TESTS.times { HASH['1234567890'] } }
results.report("constant:") { TESTS.times { HASH[KEY] } }
end
# 2000000 iterations on ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9]
# Rehearsal ---------------------------------------------
# null: 0.170000 0.000000 0.170000 ( 0.167186)
# literal: 0.970000 0.000000 0.970000 ( 0.973287)
# constant: 0.820000 0.010000 0.830000 ( 0.829517)
# ------------------------------------ total: 1.970000sec
#
# user system total real
# null: 0.160000 0.000000 0.160000 ( 0.164504)
# literal: 0.970000 0.000000 0.970000 ( 0.981910)
# constant: 0.820000 0.000000 0.820000 ( 0.825384)
# 2000000 iterations on ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin9]
# Rehearsal ---------------------------------------------
# null: 0.220000 0.000000 0.220000 ( 0.225207)
# literal: 0.800000 0.010000 0.810000 ( 0.803873)
# constant: 0.560000 0.000000 0.560000 ( 0.562713)
# ------------------------------------ total: 1.590000sec
#
# user system total real
# null: 0.230000 0.000000 0.230000 ( 0.226002)
# literal: 0.790000 0.000000 0.790000 ( 0.800224)
# constant: 0.560000 0.000000 0.560000 ( 0.558704)
# 2000000 iterations on jruby 1.3.0 java 1.8.6-287 2009-06-03
# Rehearsal ---------------------------------------------
# null: 0.246000 0.000000 0.246000 ( 0.171000)
# literal: 0.593000 0.000000 0.593000 ( 0.593000)
# constant: 0.292000 0.000000 0.292000 ( 0.293000)
# ------------------------------------ total: 1.131000sec
#
# user system total real
# null: 0.097000 0.000000 0.097000 ( 0.097000)
# literal: 0.389000 0.000000 0.389000 ( 0.389000)
# constant: 0.316000 0.000000 0.316000 ( 0.316000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment