Skip to content

Instantly share code, notes, and snippets.

@morimori
Created December 10, 2012 02:38
Show Gist options
  • Save morimori/4248055 to your computer and use it in GitHub Desktop.
Save morimori/4248055 to your computer and use it in GitHub Desktop.
require 'benchmark'
c = 100000
sa = 'abc'
sb = 'def'
sc = 'ghi'
Benchmark.bmbm do |x|
x.report('<<') do
c.times{ s = ''; s << sa << sb << sc }
end
x.report('+') do
c.times{ s = ''; s = sa + sb + sc }
end
x.report('"#{}"') do
c.times{ s = ''; s = "#{sa}#{sb}#{sc}" }
end
end
$ ruby -v concat.rb
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.0]
malloc_limit=1000000000 (8000000)
heap_min_slots=1000000 (10000)
Rehearsal -----------------------------------------
<< 0.090000 0.000000 0.090000 ( 0.088408)
+ 0.080000 0.000000 0.080000 ( 0.084944)
"#{}" 0.090000 0.000000 0.090000 ( 0.089944)
-------------------------------- total: 0.260000sec
user system total real
<< 0.090000 0.000000 0.090000 ( 0.090973)
+ 0.080000 0.000000 0.080000 ( 0.080587)
"#{}" 0.090000 0.000000 0.090000 ( 0.089658)
$ ruby -v concat.rb
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.0.0]
Rehearsal -----------------------------------------
<< 0.090000 0.000000 0.090000 ( 0.088855)
+ 0.100000 0.000000 0.100000 ( 0.104386)
"#{}" 0.110000 0.000000 0.110000 ( 0.102341)
-------------------------------- total: 0.300000sec
user system total real
<< 0.090000 0.000000 0.090000 ( 0.088120)
+ 0.100000 0.000000 0.100000 ( 0.103511)
"#{}" 0.100000 0.000000 0.100000 ( 0.102943)
$ ruby -v concat.rb
ruby 1.8.7 (2012-02-08 MBARI 8/0x6770 on patchlevel 358) [i686-darwin12.0.0], MBARI 0x6770, Ruby Enterprise Edition 2012.02
Rehearsal -----------------------------------------
<< 0.090000 0.000000 0.090000 ( 0.094898)
+ 0.070000 0.000000 0.070000 ( 0.073107)
"#{}" 0.080000 0.000000 0.080000 ( 0.083200)
-------------------------------- total: 0.240000sec
user system total real
<< 0.090000 0.000000 0.090000 ( 0.095308)
+ 0.060000 0.000000 0.060000 ( 0.065873)
"#{}" 0.080000 0.000000 0.080000 ( 0.084841)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment