public
Last active

  • Download Gist
Ruby Concat vs. Interpolate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.