Created
August 26, 2016 17:37
-
-
Save metavida/4d1b741f6fd0a0f647b8f0f31c3b44e6 to your computer and use it in GitHub Desktop.
gsub in heredoc benchmark
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
def plain | |
<<-ERR | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor | |
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud | |
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | |
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla | |
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia | |
deserunt mollit anim id est laborum. | |
ERR | |
end | |
def squigly | |
<<~ERR | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor | |
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud | |
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | |
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla | |
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia | |
deserunt mollit anim id est laborum. | |
ERR | |
end | |
def gsubed | |
<<-ERR.gsub(/^\s+/,'').gsub(/\n/, ' ') | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor | |
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud | |
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | |
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla | |
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia | |
deserunt mollit anim id est laborum. | |
ERR | |
end | |
def tr | |
<<-ERR.gsub(/^\s+/,'').tr("\n", ' ') | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor | |
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud | |
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | |
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla | |
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia | |
deserunt mollit anim id est laborum. | |
ERR | |
end | |
require 'benchmark' | |
puts "Runing benchmark in Ruby #{RUBY_VERSION}" | |
plain | |
raise "squigly != gsubed\n\n#{squigly}\n\n#{gsubed}" if squigly != gsubed rescue nil | |
raise "gsubed != tr\n\n#{gsubed}\n\n#{tr}" if gsubed != tr | |
n = 100000 | |
Benchmark.bm(10) do |x| | |
x.report('plain') { n.times do; plain; end } | |
if (squigly rescue nil) | |
x.report('squigly') { n.times do; squigly; end } | |
else | |
puts "squigly not supported in Ruby #{RUBY_VERSION}" | |
end | |
x.report('gsubed') { n.times do; gsubed; end } | |
x.report('tr') { n.times do; tr; end } | |
end |
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
$ rvm 2.3.1 do ruby benchmark_gsub_heredoc.rb | |
Runing benchmark in Ruby 2.3.1 | |
user system total real | |
plain 0.010000 0.000000 0.010000 ( 0.011866) | |
squigly 0.010000 0.000000 0.010000 ( 0.010107) | |
gsubed 1.820000 0.030000 1.850000 ( 1.867834) | |
tr 1.310000 0.020000 1.330000 ( 1.352448) | |
# I then commented out the squigly method and ran with other ruby versions | |
$ rvm 2.2.3,2.1.8,1.9.3-p551 do ruby benchmark_gsub_heredoc.rb | |
Runing benchmark in Ruby 2.2.3 | |
user system total real | |
plain 0.020000 0.000000 0.020000 ( 0.017694) | |
squigly not supported in Ruby 2.2.3 | |
gsubed 2.630000 0.030000 2.660000 ( 2.706699) | |
tr 2.220000 0.030000 2.250000 ( 2.305155) | |
Runing benchmark in Ruby 2.1.8 | |
user system total real | |
plain 0.020000 0.000000 0.020000 ( 0.019071) | |
squigly not supported in Ruby 2.1.8 | |
gsubed 2.950000 0.040000 2.990000 ( 3.074034) | |
tr 2.380000 0.030000 2.410000 ( 2.476173) | |
Runing benchmark in Ruby 1.9.3 | |
user system total real | |
plain 0.020000 0.000000 0.020000 ( 0.021079) | |
squigly not supported in Ruby 1.9.3 | |
gsubed 1.170000 0.020000 1.190000 ( 1.225133) | |
tr 0.960000 0.020000 0.980000 ( 0.991184) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment