Skip to content

Instantly share code, notes, and snippets.

@triangletodd
Created September 23, 2016 20:33
Show Gist options
  • Save triangletodd/de0c704d699bf43164ae24bc8d25fd3d to your computer and use it in GitHub Desktop.
Save triangletodd/de0c704d699bf43164ae24bc8d25fd3d to your computer and use it in GitHub Desktop.
Ruby tr vs. gsub
#!/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