Skip to content

Instantly share code, notes, and snippets.

@bdeterling
Created February 3, 2011 23:35
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 bdeterling/810463 to your computer and use it in GitHub Desktop.
Save bdeterling/810463 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 1000000
m = n.to_s
Benchmark.bm do |x|
x.report("assign single") { n.times do; c = 'a string'; end}
x.report("assign double") { n.times do; c = "a string"; end}
x.report("assign interp") { n.times do; c = "a #{n} string"; end}
x.report("concat single") { n.times do; 'a ' + n.to_s + ' string b ' + n.to_s + ' string'; end}
x.report("concat double") { n.times do; "a " + n.to_s + " string b " + n.to_s + " string"; end}
x.report("concat interp") { n.times do; "a #{n} string b #{n} string"; end}
x.report("single optim") { n.times do; 'a ' + m + ' string b ' + m + ' string'; end}
x.report("double optim") { n.times do; "a " + m + " string b " + m + " string"; end}
x.report("interp optim") { n.times do; "a #{m} string b #{m} string"; end}
end
1.9.2
-----
user system total real
assign single 0.170000 0.000000 0.170000 ( 0.165743)
assign double 0.160000 0.000000 0.160000 ( 0.164564)
assign interp 0.650000 0.010000 0.660000 ( 0.716917)
concat single 1.660000 0.000000 1.660000 ( 1.674781)
concat double 1.680000 0.000000 1.680000 ( 1.695005)
concat interp 1.320000 0.010000 1.330000 ( 1.321017)
single optim 1.240000 0.000000 1.240000 ( 1.245957)
double optim 1.210000 0.000000 1.210000 ( 1.218838)
interp optim 0.860000 0.000000 0.860000 ( 0.854629)
1.8.7
-----
user system total real
assign single 0.250000 0.000000 0.250000 ( 0.250248)
assign double 0.240000 0.000000 0.240000 ( 0.244231)
assign interp 1.220000 0.010000 1.230000 ( 1.287550)
concat single 3.100000 0.010000 3.110000 ( 3.246304)
concat double 3.040000 0.010000 3.050000 ( 3.153448)
concat interp 1.970000 0.000000 1.970000 ( 1.987011)
single optim 2.040000 0.010000 2.050000 ( 2.138786)
double optim 2.000000 0.000000 2.000000 ( 2.021183)
interp optim 0.940000 0.000000 0.940000 ( 0.947649)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment