Skip to content

Instantly share code, notes, and snippets.

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 jonas-schulze/d8ba86b81660090272e6cc2efec0a52c to your computer and use it in GitHub Desktop.
Save jonas-schulze/d8ba86b81660090272e6cc2efec0a52c to your computer and use it in GitHub Desktop.
Ruby Performance Tricks -- 6 Years Later -- Trick 2
require 'benchmark'
N = 1000
BASIC_LENGTH = 10
puts RUBY_DESCRIPTION
5.times do |factor|
length = BASIC_LENGTH * (10 ** factor)
puts "_" * 60 + "\nLENGTH: #{length}"
Benchmark.bm(10, '+= VS <<', '#{} VS <<') do |x|
concat_report = x.report("+=") do
str1 = ""
str2 = "s" * length
N.times { str1 += str2 }
end
modify_report = x.report("<<") do
str1 = "s"
str2 = "s" * length
N.times { str1 << str2 }
end
interpolate_report = x.report('#{}') do
str1 = "s"
str2 = "s" * length
N.times { str1 = "#{str1}#{str2}" }
end
[concat_report / modify_report, interpolate_report / modify_report]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment