Skip to content

Instantly share code, notes, and snippets.

@tgautier-silicon
Created April 4, 2012 19:50
Show Gist options
  • Save tgautier-silicon/2305095 to your computer and use it in GitHub Desktop.
Save tgautier-silicon/2305095 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 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
============= Results
[~/Desktop]$ ruby quotes_and_string_love.rb [ruby-1.9.3-p125]
user system total real
assign single 0.130000 0.000000 0.130000 ( 0.128500)
assign double 0.120000 0.000000 0.120000 ( 0.127298)
assign interp 0.510000 0.000000 0.510000 ( 0.502982)
concat single 1.520000 0.000000 1.520000 ( 1.520487)
concat double 1.520000 0.000000 1.520000 ( 1.520864)
concat interp 1.140000 0.000000 1.140000 ( 1.146863)
single optim 1.140000 0.000000 1.140000 ( 1.141740)
double optim 1.150000 0.000000 1.150000 ( 1.150964)
interp optim 0.760000 0.000000 0.760000 ( 0.752978)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment