Skip to content

Instantly share code, notes, and snippets.

@sixfeetover
Created March 8, 2012 21:20
Show Gist options
  • Save sixfeetover/2003508 to your computer and use it in GitHub Desktop.
Save sixfeetover/2003508 to your computer and use it in GitHub Desktop.
Compare [].join to string interpolation
# Ruby 1.9.3-p125
require 'benchmark'
variable = 'string'
integer = 5
n = 1_000_000
Benchmark.bm do |x|
x.report("array:") do
n.times do
[:part, variable, integer].join(':')
end
end
x.report("string:") do
n.times do
"#{:part}:#{variable}:#{integer}"
end
end
end
# Ruby 1.9.2-p290
# n = 1_000_000
user system total real
array: 1.330000 0.000000 1.330000 ( 1.327477)
string: 0.790000 0.000000 0.790000 ( 0.794048)
# Ruby 1.9.2-p290
# n = 10_000_000
user system total real
array: 12.950000 0.010000 12.960000 ( 12.950889)
string: 7.850000 0.020000 7.870000 ( 7.862994)
# Ruby 1.9.3-p0
# n = 10_000_000
user system total real
array: 13.230000 0.010000 13.240000 ( 13.235264)
string: 7.900000 0.010000 7.910000 ( 7.906731)
# Ruby 1.9.3-p125
# n = 10_000_000
user system total real
array: 13.270000 0.000000 13.270000 ( 13.277366)
string: 8.190000 0.000000 8.190000 ( 8.190424)
# Ruby 1.9.3-p125 falcon patch (https://gist.github.com/1688857)
# n = 10_000_000
user system total real
array 33.470000 0.010000 33.480000 ( 33.485981)
str1 31.130000 0.000000 31.130000 ( 31.132234)
str2 31.570000 0.010000 31.580000 ( 31.589953)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment