Skip to content

Instantly share code, notes, and snippets.

@mloughran
Created November 30, 2010 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloughran/721430 to your computer and use it in GitHub Desktop.
Save mloughran/721430 to your computer and use it in GitHub Desktop.
Appending to a string vs stringIO
require 'rubygems'
require 'benchmark'
require 'stringio'
Benchmark.bm do |x|
n = 1000000
x.report('string') do
string = ''
appended = 'b' * 100
n.times { string << appended }
end
x.report('stringio') do
string = StringIO.new
appended = 'b' * 100
n.times { string << appended }
end
end
# user system total real
# string 0.360000 0.090000 0.450000 ( 0.441618)
# stringio 0.440000 0.100000 0.540000 ( 0.544054)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment