Skip to content

Instantly share code, notes, and snippets.

@keichan34
Created August 26, 2015 00:22
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 keichan34/e48bb35efc37b0861095 to your computer and use it in GitHub Desktop.
Save keichan34/e48bb35efc37b0861095 to your computer and use it in GitHub Desktop.
~/Desktop > ruby --version
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
~/Desktop > ruby ./concat_benchmark.rb
Calculating -------------------------------------
Interpolation 81.309k i/100ms
Array + join 65.714k i/100ms
String concatenation 62.816k i/100ms
String mutation 68.529k i/100ms
-------------------------------------------------
Interpolation 1.843M (± 6.0%) i/s - 9.188M
Array + join 1.218M (± 5.8%) i/s - 6.111M
String concatenation 1.087M (± 5.1%) i/s - 5.465M
String mutation 1.248M (± 5.0%) i/s - 6.236M
#!/usr/bin/env ruby
require 'benchmark/ips'
STRING = "hello there".freeze
Benchmark.ips do |x|
x.report("Interpolation") do
"#{STRING}-#{STRING}-#{STRING}"
end
x.report("Array + join") do
[STRING, STRING, STRING].join("-")
end
x.report("String concatenation") do
STRING + "-" + STRING + "-" + STRING
end
x.report("String mutation") do
str = ""
str << STRING
str << "-"
str << STRING
str << "-"
str << STRING
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment