Skip to content

Instantly share code, notes, and snippets.

@rklemme
Last active December 17, 2015 02:09
Show Gist options
  • Save rklemme/5533719 to your computer and use it in GitHub Desktop.
Save rklemme/5533719 to your computer and use it in GitHub Desktop.
Quick benchmark of string appending
Rehearsal ------------------------------------------------------------------
interpol 0.218000 0.000000 0.218000 ( 0.218013)
plus 0.156000 0.000000 0.156000 ( 0.158009)
append 0.390000 0.000000 0.390000 ( 0.386022)
const interpol 0.203000 0.000000 0.203000 ( 0.193011)
const plus 0.218000 0.000000 0.218000 ( 0.231013)
const append 0.203000 0.000000 0.203000 ( 0.196011)
--------------------------------------------------------- total: 1.388000sec
user system total real
interpol 0.219000 0.000000 0.219000 ( 0.213012)
plus 0.156000 0.000000 0.156000 ( 0.156009)
append 0.374000 0.000000 0.374000 ( 0.383022)
const interpol 0.203000 0.000000 0.203000 ( 0.191011)
const plus 0.218000 0.000000 0.218000 ( 0.224013)
const append 0.187000 0.000000 0.187000 ( 0.193011)
require 'benchmark'
REP = 1_000_000
a = "foo".freeze
b = "bar".freeze
Benchmark.bmbm 30 do |x|
x.report "interpol" do
REP.times { "#{a}#{b}" }
end
x.report "plus" do
REP.times { a + b }
end
x.report "append" do
REP.times { a.dup << b }
end
x.report "const interpol" do
REP.times { "foo#{b}" }
end
x.report "const plus" do
REP.times { 'foo' + b }
end
x.report "const append" do
REP.times { 'foo' << b }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment