Skip to content

Instantly share code, notes, and snippets.

@mkonikowski
Forked from tgautier-silicon/gist:2305095
Created April 5, 2012 08:18
Show Gist options
  • Save mkonikowski/2308961 to your computer and use it in GitHub Desktop.
Save mkonikowski/2308961 to your computer and use it in GitHub Desktop.
Le point sur l'interpolation
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 << 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("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
#============= Results
# [ruby-1.9.3-p125@]
$ ruby quotes_and_string_love.rb
user system total real
assign single 0.110000 0.000000 0.110000 ( 0.116910)
assign double 0.120000 0.000000 0.120000 ( 0.117053)
assign interp 0.480000 0.000000 0.480000 ( 0.478378)
concat + single 1.350000 0.000000 1.350000 ( 1.352242)
concat + double 1.350000 0.000000 1.350000 ( 1.350185)
concat << single 1.110000 0.000000 1.110000 ( 1.109680)
concat << double 1.110000 0.000000 1.110000 ( 1.108533)
concat interp 1.020000 0.000000 1.020000 ( 1.022490)
single + optim 1.010000 0.000000 1.010000 ( 1.010864)
double + optim 1.010000 0.000000 1.010000 ( 1.009166)
single << optim 0.740000 0.000000 0.740000 ( 0.734263)
double << optim 0.730000 0.000000 0.730000 ( 0.731785)
interp optim 0.650000 0.000000 0.650000 ( 0.647658)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment