Created
September 23, 2016 20:33
-
-
Save triangletodd/de0c704d699bf43164ae24bc8d25fd3d to your computer and use it in GitHub Desktop.
Ruby tr vs. gsub
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
#!/usr/bin/env ruby | |
require 'benchmark/ips' | |
SLUG = 'writing-fast-ruby' | |
def slow | |
SLUG.gsub('-', ' ') | |
end | |
def fast | |
SLUG.tr('-', ' ') | |
end | |
Benchmark.ips do |x| | |
x.report('String#gsub') { slow } | |
x.report('String#tr') { fast } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment