Skip to content

Instantly share code, notes, and snippets.

@deadkarma
Created May 7, 2013 14:11
Show Gist options
  • Save deadkarma/5532846 to your computer and use it in GitHub Desktop.
Save deadkarma/5532846 to your computer and use it in GitHub Desktop.
String performance
require 'benchmark'
n = 1000000
Benchmark.bmbm 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("concat single") { n.times do; 'a string ' + 'b string'; end}
x.report("concat double") { n.times do; "a string " + "b string"; end}
x.report("use << single") { n.times do; 'a string ' << 'b string'; end}
x.report("use << double") { n.times do; "a string " << "b string"; end}
x.report("use interpola") { n.times do; "a string #{'b_string'}"; end}
end
@deadkarma
Copy link
Author

Rehearsal -------------------------------------------------
assign single   0.180000   0.000000   0.180000 (  0.180115)
assign double   0.170000   0.000000   0.170000 (  0.175267)
concat single   0.560000   0.000000   0.560000 (  0.560109)
concat double   0.570000   0.000000   0.570000 (  0.568490)
use << single   0.670000   0.000000   0.670000 (  0.672556)
use << double   0.680000   0.000000   0.680000 (  0.674999)
use interpola   0.150000   0.000000   0.150000 (  0.146872)
---------------------------------------- total: 2.980000sec

                    user     system      total        real
assign single   0.180000   0.000000   0.180000 (  0.180616)
assign double   0.180000   0.000000   0.180000 (  0.184809)
concat single   0.580000   0.000000   0.580000 (  0.579451)
concat double   0.570000   0.000000   0.570000 (  0.570579)
use << single   0.680000   0.000000   0.680000 (  0.675467)
use << double   0.670000   0.000000   0.670000 (  0.673734)
use interpola   0.150000   0.000000   0.150000 (  0.150993)
[Finished in 6.0s]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment