Last active
March 15, 2018 11:03
-
-
Save jonas-schulze/d8ba86b81660090272e6cc2efec0a52c to your computer and use it in GitHub Desktop.
Ruby Performance Tricks -- 6 Years Later -- Trick 2
This file contains 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' | |
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