Last active
August 29, 2015 14:17
-
-
Save duplamatyi/169241ce3949aa2ac472 to your computer and use it in GitHub Desktop.
Ruby String Concat Benchmark Fun
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' | |
iterations = 100000 | |
api_base = 'https://api.example.com' | |
api_version = 'v1' | |
rest_url = 'resource' | |
puts 'Ruby String Concat Benchmark Fun' | |
puts '====================================================' | |
5.times do | |
Benchmark.bm(10) do |benchmark| | |
benchmark.report(' add: +') do | |
iterations.times do | |
url = api_base + '/' + api_version + '/' + rest_url | |
end | |
end | |
benchmark.report(' append: <<') do | |
iterations.times do | |
url = '' | |
url << api_base << '/' << api_version << '/' << rest_url | |
end | |
end | |
benchmark.report('interpolate: #{}') do | |
iterations.times do | |
url = "#{api_base}/#{api_version}/#{rest_url}" | |
end | |
end | |
end | |
end | |
puts '====================================================' | |
=begin | |
Ruby String Concat Benchmark Fun | |
==================================================== | |
user system total real | |
add: + 0.100000 0.000000 0.100000 ( 0.102474) | |
append: << 0.070000 0.010000 0.080000 ( 0.067541) | |
interpolate: #{} 0.050000 0.000000 0.050000 ( 0.055584) | |
user system total real | |
add: + 0.100000 0.000000 0.100000 ( 0.096606) | |
append: << 0.060000 0.000000 0.060000 ( 0.061436) | |
interpolate: #{} 0.050000 0.000000 0.050000 ( 0.052648) | |
user system total real | |
add: + 0.110000 0.000000 0.110000 ( 0.109778) | |
append: << 0.060000 0.000000 0.060000 ( 0.062491) | |
interpolate: #{} 0.060000 0.000000 0.060000 ( 0.057211) | |
user system total real | |
add: + 0.100000 0.000000 0.100000 ( 0.101370) | |
append: << 0.060000 0.000000 0.060000 ( 0.064502) | |
interpolate: #{} 0.060000 0.000000 0.060000 ( 0.059637) | |
user system total real | |
add: + 0.110000 0.010000 0.120000 ( 0.112746) | |
append: << 0.060000 0.000000 0.060000 ( 0.064556) | |
interpolate: #{} 0.060000 0.000000 0.060000 ( 0.055275) | |
==================================================== | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, you might want to look at
Benchmark.bmbm
for the rehearsal phase. Or maybe usebenchmark-ips
.