Skip to content

Instantly share code, notes, and snippets.

@codegoalie
Created November 20, 2013 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegoalie/7570180 to your computer and use it in GitHub Desktop.
Save codegoalie/7570180 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 500_000
Benchmark.bm(7) do |x|
x.report('append:') do
a = ''
n.times { a << 'a' }
end
x.report('+=:') do
a = ''
n.times { a += 'a' }
end
x.report('=+:') do
a = ''
n.times { a = a + 'a' }
end
end
@codegoalie
Copy link
Author

$ ruby -w string_append_vs_plus_equals.rb             [14:31:14]
              user     system      total        real
append:   0.100000   0.000000   0.100000 (  0.101719)
+=:      27.500000  14.360000  41.860000 ( 41.860157)
=+:      27.110000  14.020000  41.130000 ( 41.131695)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment