Created
April 13, 2012 23:51
-
-
Save mtkd/2381053 to your computer and use it in GitHub Desktop.
String Concatenation Benchmarks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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