Skip to content

Instantly share code, notes, and snippets.

@yfeldblum
Created December 15, 2012 18:25
Show Gist options
  • Save yfeldblum/edee751c0eddb7a855b8 to your computer and use it in GitHub Desktop.
Save yfeldblum/edee751c0eddb7a855b8 to your computer and use it in GitHub Desktop.
Benching strings in ruby-1.9.3-p327 with falcon-gc patch.
require "benchmark"
def s(s) ; s ; end
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("string-literal-in-method") { times.times {
s "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.720000 0.010000 0.730000 ( 0.732522)
string-literal 2.000000 0.000000 2.000000 ( 2.011417)
string-literal-in-method 2.370000 0.000000 2.370000 ( 2.380874)
symbol-literal 2.400000 0.000000 2.400000 ( 2.399494)
symbol-string-cache 0.990000 0.000000 0.990000 ( 0.997033)
--------------------------------------------------- total: 8.490000sec
user system total real
constant 0.730000 0.000000 0.730000 ( 0.727206)
string-literal 1.770000 0.010000 1.780000 ( 1.770566)
string-literal-in-method 2.270000 0.000000 2.270000 ( 2.282503)
symbol-literal 2.210000 0.000000 2.210000 ( 2.210457)
symbol-string-cache 1.000000 0.000000 1.000000 ( 1.002717)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment