Created
May 5, 2020 20:56
-
-
Save maxcal/87d022d183f87513a7669b1fda3d0911 to your computer and use it in GitHub Desktop.
The great TR vs GSUB battle
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' | |
def vowel_cipher(string) | |
string.tr "aAeEiIoOuU", "eEiIoOuUaA" | |
end | |
NEXT_VOWEL = {"a"=>"e", "A"=>"E", "e"=>"i", "E"=>"I", "i"=>"o", | |
"I"=>"O", "o"=>"u", "O"=>"U", "u"=>"a", "U"=>"A"} | |
def vowel_cipher_gsub(str) | |
str.gsub(/[aeiou]/i, NEXT_VOWEL) | |
end | |
Benchmark.bm do |x| | |
x.report { 1000000.times { vowel_cipher("Paper Cup") } } | |
x.report { 1000000.times { vowel_cipher_gsub("Paper Cup") } } | |
end | |
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
user system total real | |
2.871711 0.000000 2.871711 ( 3.027596) | |
5.298556 0.000000 5.298556 ( 5.449371) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment