Skip to content

Instantly share code, notes, and snippets.

@mtkd
Created April 13, 2012 23:51
Show Gist options
  • Save mtkd/2381053 to your computer and use it in GitHub Desktop.
Save mtkd/2381053 to your computer and use it in GitHub Desktop.
String Concatenation Benchmarks
require 'benchmark'
first_name = "first"
last_name = "last"
Benchmark.bm do |x|
x.report { 100000.times do; "#{first_name} #{last_name}" ; end; }
x.report { 100000.times do; [first_name, last_name].join(' ') ; end; }
x.report { 100000.times do; '%s %s' % [first_name, last_name] ; end; }
x.report { 100000.times do; first_name + ' ' + last_name ; end; }
end
# user system total real
# 0.040000 0.000000 0.040000 ( 0.042028)
# 0.090000 0.000000 0.090000 ( 0.087404)
# 0.120000 0.000000 0.120000 ( 0.126011)
# 0.040000 0.000000 0.040000 ( 0.040019)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment