Skip to content

Instantly share code, notes, and snippets.

@norman
Created September 4, 2011 02:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save norman/1192140 to your computer and use it in GitHub Desktop.
Save norman/1192140 to your computer and use it in GitHub Desktop.
string interpolation vs array join
require "benchmark"
puts %x{ruby -v} + "\n"
Benchmark.bmbm do |x|
N = 1_000_000
a = "aa"
b = "bb"
c = "cc"
x.report 'string interpolation' do
N.times {"#{a}-#{b}-#{c}"}
end
x.report 'array joining' do
N.times {[a, b, c].join("-")}
end
end
print "\n"
jruby 1.6.3 (ruby-1.8.7-p330) (2011-07-07 965162f) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_26) [darwin-x86_64-java]
Rehearsal --------------------------------------------------------
string interpolation 0.331000 0.000000 0.331000 ( 0.331000)
array joining 0.352000 0.000000 0.352000 ( 0.352000)
----------------------------------------------- total: 0.683000sec
user system total real
string interpolation 0.287000 0.000000 0.287000 ( 0.287000)
array joining 0.351000 0.000000 0.351000 ( 0.351000)
rubinius 1.2.5dev (1.8.7 9335027b yyyy-mm-dd JI) [x86_64-apple-darwin11.0.0]
Rehearsal --------------------------------------------------------
string interpolation 0.276763 0.008056 0.284819 ( 0.283093)
array joining 2.530047 0.008351 2.538398 ( 2.452822)
----------------------------------------------- total: 2.823217sec
user system total real
string interpolation 0.210928 0.000407 0.211335 ( 0.211379)
array joining 1.902922 0.003008 1.905930 ( 1.906076)
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin11.0.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03
Rehearsal --------------------------------------------------------
string interpolation 1.200000 0.000000 1.200000 ( 1.201270)
array joining 1.310000 0.000000 1.310000 ( 1.320347)
----------------------------------------------- total: 2.510000sec
user system total real
string interpolation 1.200000 0.000000 1.200000 ( 1.205013)
array joining 1.290000 0.000000 1.290000 ( 1.282182)
ruby 1.8.6 (2010-09-02 patchlevel 420) [i686-darwin11.1.0]
Rehearsal --------------------------------------------------------
string interpolation 1.080000 0.000000 1.080000 ( 1.080896)
array joining 1.050000 0.000000 1.050000 ( 1.051609)
----------------------------------------------- total: 2.130000sec
user system total real
string interpolation 1.060000 0.000000 1.060000 ( 1.058468)
array joining 1.150000 0.000000 1.150000 ( 1.183906)
ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.0.0]
Rehearsal --------------------------------------------------------
string interpolation 1.180000 0.010000 1.190000 ( 1.239572)
array joining 1.180000 0.000000 1.180000 ( 1.189541)
----------------------------------------------- total: 2.370000sec
user system total real
string interpolation 1.140000 0.000000 1.140000 ( 1.138641)
array joining 1.180000 0.000000 1.180000 ( 1.193472)
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
Rehearsal --------------------------------------------------------
string interpolation 0.820000 0.000000 0.820000 ( 0.918609)
array joining 1.060000 0.010000 1.070000 ( 1.066198)
----------------------------------------------- total: 1.890000sec
user system total real
string interpolation 0.830000 0.000000 0.830000 ( 0.849953)
array joining 1.160000 0.000000 1.160000 ( 1.195687)
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
Rehearsal --------------------------------------------------------
string interpolation 0.840000 0.010000 0.850000 ( 0.924422)
array joining 1.100000 0.000000 1.100000 ( 1.126441)
----------------------------------------------- total: 1.950000sec
user system total real
string interpolation 0.810000 0.000000 0.810000 ( 0.820044)
array joining 1.140000 0.010000 1.150000 ( 1.285240)
ruby 1.9.3dev (2011-07-31 revision 32789) [x86_64-darwin11.0.0]
Rehearsal --------------------------------------------------------
string interpolation 0.850000 0.000000 0.850000 ( 0.878875)
array joining 1.200000 0.010000 1.210000 ( 1.287827)
----------------------------------------------- total: 2.060000sec
user system total real
string interpolation 0.830000 0.000000 0.830000 ( 0.838500)
array joining 1.190000 0.020000 1.210000 ( 1.244710)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment