public
Created

Different ways to build a 100MB string

  • Download Gist
string-benchmark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.