Skip to content

Instantly share code, notes, and snippets.

@elandesign
Created October 11, 2012 16:14
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 elandesign/3873528 to your computer and use it in GitHub Desktop.
Save elandesign/3873528 to your computer and use it in GitHub Desktop.
Different ways to build a 100MB string
require 'benchmark'
require 'stringio'
Benchmark.measure do
string = ''
100_000_000.times do
string << '0'
end
end
# => 17.530000 0.040000 17.570000 ( 17.794845)
Benchmark.measure do
array = []
100_000_000.times do
array << '0'
end
array.join
end
# => 62.730000 1.410000 64.140000 ( 64.138629)
Benchmark.measure do
io = StringIO.new
100_000_000.times do
io.write '0'
end
io.rewind
io.read
end
# => 167.350000 2.020000 169.370000 (169.494641)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment