Created
October 11, 2012 16:14
-
-
Save elandesign/3873528 to your computer and use it in GitHub Desktop.
Different ways to build a 100MB string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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