Skip to content

Instantly share code, notes, and snippets.

/bench.rb Secret

Created December 15, 2012 18:00
Show Gist options
  • Save anonymous/536434e2c3adba9070df to your computer and use it in GitHub Desktop.
Save anonymous/536434e2c3adba9070df to your computer and use it in GitHub Desktop.
Benching strings
require "benchmark"
Benchmark.bmbm do |bm|
times = 10_000_000
THESTRING = "asdf1234asdf1234asdf1234asdf1234"
bm.report("constant") { times.times {
THESTRING
} }
bm.report("string-literal") { times.times {
"asdf1234asdf1234asdf1234asdf1234"
} }
bm.report("symbol-literal") { times.times {
:asdf1234asdf1234asdf1234asdf1234.to_s
} }
cache = {:asdf1234asdf1234asdf1234asdf1234 => THESTRING}
bm.report("symbol-string-cache") { times.times {
cache[:asdf1234asdf1234asdf1234asdf1234]
} }
end
Rehearsal -------------------------------------------------------
constant 0.730000 0.000000 0.730000 ( 0.726672)
string-literal 1.740000 0.000000 1.740000 ( 1.749538)
symbol-literal 2.260000 0.000000 2.260000 ( 2.259165)
symbol-string-cache 1.020000 0.000000 1.020000 ( 1.015073)
---------------------------------------------- total: 5.750000sec
user system total real
constant 0.720000 0.000000 0.720000 ( 0.722902)
string-literal 1.730000 0.000000 1.730000 ( 1.730661)
symbol-literal 2.370000 0.000000 2.370000 ( 2.379667)
symbol-string-cache 1.030000 0.000000 1.030000 ( 1.026997)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment