Skip to content

Instantly share code, notes, and snippets.

@pfleidi
Last active October 7, 2015 09:39
Show Gist options
  • Save pfleidi/c376b6e16d3aa8cdcdfb to your computer and use it in GitHub Desktop.
Save pfleidi/c376b6e16d3aa8cdcdfb to your computer and use it in GitHub Desktop.
require 'stringio'
require 'benchmark/ips'
list = 1...10_000
Benchmark.ips do |x|
x.config(:time => 10, :warmup => 2)
x.report('String#<<') do
s = ''
list.each { |x| s << "#{x}\n" }
end
x.report('String#+') do
s = ''
list.each { |x| s += "#{x}\n" }
end
x.report('StringIO.open') do
StringIO.open do |s|
list.each { |x| s.puts(x) }
s.string
end
end
end
# Calculating -------------------------------------
# String#<< 24.000 i/100ms
# String#+ 1.000 i/100ms
# StringIO.open 14.000 i/100ms
# -------------------------------------------------
# String#<< 236.078 (± 5.1%) i/s - 2.376k
# String#+ 15.901 (± 6.3%) i/s - 159.000
# StringIO.open 134.767 (± 6.7%) i/s - 1.344k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment