Last active
September 11, 2015 08:31
-
-
Save abhinavmsra/7a13bd538146f9f1035a to your computer and use it in GitHub Desktop.
Testing string operations in ruby
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' | |
iterations = 100_000 | |
puts 'With Static Strings' | |
puts '======================================================' | |
Benchmark.bm do |bm| | |
bm.report('joining an array of strings') do | |
iterations.times do | |
['Testing', 'String', 'Operations', 'in', 'Ruby'].join(' ') | |
end | |
end | |
bm.report('string interpolation') do | |
iterations.times do | |
"Testing String Operations in Ruby" | |
end | |
end | |
bm.report('string concatenation') do | |
iterations.times do | |
'Testing' + 'String' + 'Operations' + 'in' + 'Ruby' | |
end | |
end | |
bm.report('string concatenation without plus sign') do | |
iterations.times do | |
'Testing ' 'String ' 'Operations ' 'in ' 'Ruby ' | |
end | |
end | |
bm.report('using << sign') do | |
iterations.times do | |
'Testing ' << 'String ' << 'Operations ' << 'in ' << 'Ruby ' | |
end | |
end | |
bm.report('using concat') do | |
iterations.times do | |
'Testing '.concat('String ').concat('Operations ').concat('in ').concat('Ruby ') | |
end | |
end | |
end | |
puts 'With dynamic Strings' | |
puts '=======================================================' | |
string_1 = 'Testing' | |
string_2 = 'String' | |
string_3 = 'Operations' | |
string_4 = 'in' | |
string_5 = 'Ruby' | |
Benchmark.bm do |bm| | |
bm.report('joining an array of strings') do | |
iterations.times do | |
[string_1, string_2, string_3, string_4, string_5].join(' ') | |
end | |
end | |
bm.report('string interpolation') do | |
iterations.times do | |
"#{string_1} #{string_2} #{string_3} #{string_4} #{string_5}" | |
end | |
end | |
bm.report('string concatenation') do | |
iterations.times do | |
string_1 + ' ' + string_2 + ' ' + string_3 + ' ' + string_4 + ' ' + string_5 | |
end | |
end | |
bm.report('using << sign') do | |
iterations.times do | |
string_1 << ' ' << string_2 << ' ' << string_3 << ' ' << string_4 << ' ' << string_5 | |
end | |
end | |
bm.report('using concat') do | |
iterations.times do | |
string_1.concat(' ').concat(string_2).concat(' ').concat(string_3) | |
.concat(' ').concat(string_4).concat(' ').concat(string_5) | |
end | |
end | |
end | |
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< OUTPUT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
With Static Strings | |
====================================================== | |
user system total real | |
joining an array of strings 0.160000 0.000000 0.160000 ( 0.166476) | |
string interpolation 0.020000 0.000000 0.020000 ( 0.013685) | |
string concatenation 0.120000 0.000000 0.120000 ( 0.125947) | |
string concatenation without plus sign 0.020000 0.000000 0.020000 ( 0.013228) | |
using << sign 0.090000 0.000000 0.090000 ( 0.098427) | |
using concat 0.110000 0.000000 0.110000 ( 0.102523) | |
With dynamic Strings | |
======================================================= | |
user system total real | |
joining an array of strings 0.120000 0.000000 0.120000 ( 0.121523) | |
string interpolation 0.080000 0.000000 0.080000 ( 0.079512) | |
string concatenation 0.230000 0.000000 0.230000 ( 0.234641) | |
using << sign 0.110000 0.000000 0.110000 ( 0.108388) | |
using concat 0.120000 0.000000 0.120000 ( 0.118256) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment