Skip to content

Instantly share code, notes, and snippets.

@andrewhr
Last active August 29, 2015 14:16
Show Gist options
  • Save andrewhr/fa067d213c568015ff18 to your computer and use it in GitHub Desktop.
Save andrewhr/fa067d213c568015ff18 to your computer and use it in GitHub Desktop.
Uso de gsub vs operadores de regex para limpeza (opcional) de dados.
require 'benchmark/ips'
MATCH = '123.123.123-99'
NON_MATCH = 'João da Silva'
CPF_REGEX = /\A(\d{3})\.?(\d{3})\.?(\d{3})\-?(\d{2})\z/
def do_gsub(candidate)
candidate.gsub(CPF_REGEX, '\1\2\3\4')
end
def do_perl(candidate)
if candidate =~ CPF_REGEX
"#{$1}#{$2}#{$3}#{$4}"
else
candidate
end
end
Benchmark.ips do |x|
x.report("match gsub") { do_gsub(MATCH) }
x.report("non-match gsub") { do_gsub(NON_MATCH) }
x.report("match perl") { do_perl(MATCH) }
x.report("non-match perl") { do_perl(NON_MATCH) }
x.compare! # cortesia do andre :)
end
# $ ruby regex.rb
# Calculating -------------------------------------
# match gsub 12.489k i/100ms
# non-match gsub 36.590k i/100ms
# match perl 20.082k i/100ms
# non-match perl 56.169k i/100ms
# -------------------------------------------------
# match gsub 152.499k (±12.8%) i/s - 749.340k
# non-match gsub 943.747k (±20.3%) i/s - 4.427M
# match perl 297.897k (±12.4%) i/s - 1.466M
# non-match perl 1.363M (±19.6%) i/s - 6.459M
#
# Comparison:
# non-match perl: 1362968.9 i/s
# non-match gsub: 943746.8 i/s - 1.44x slower
# match perl: 297897.2 i/s - 4.58x slower
# match gsub: 152499.3 i/s - 8.94x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment