Skip to content

Instantly share code, notes, and snippets.

@ggcampinho
Last active August 29, 2015 14:26
Show Gist options
  • Save ggcampinho/dcfe409d870555c4ea56 to your computer and use it in GitHub Desktop.
Save ggcampinho/dcfe409d870555c4ea56 to your computer and use it in GitHub Desktop.
Benchmark between match an email domain in a regexp versus an array
Calculating -------------------------------------
array 45.561k i/100ms
regexp 48.843k i/100ms
-------------------------------------------------
array 711.188k (± 5.8%) i/s - 3.554M
regexp 797.338k (± 5.9%) i/s - 4.005M
Comparison:
regexp: 797337.6 i/s
array: 711187.6 i/s - 1.12x slower
require 'benchmark/ips'
domains = %w(ceuma.br estacio.br fumec.br grad.ufsc.br mackenzista.com.br puc-rio.br pucgoias.edu.br pucminas.br pucpr.br pucrs.br pucsp.br tonyventura.com.br ucb.br ucsal.br uea.edu.br uece.br ueg.br uemg.br uergs.edu.br uerj.br ufam.edu.br ufba.br ufc.br ufcspa.edu.br ufg.br ufma.br ufmg.br ufms.br ufmt.br ufpa.br ufpe.br ufpi.edu.br ufpr.br ufra.edu.br ufrgs.br ufs.br unama.br unb.br uneb.br unicap.br unifacs.br unifacs.br unifap.br unifenas.br unifesp.br unifor.br uniniltonlins.edu.br unir.br unirio.br unisinos.br up.com.br usf.edu.br usp.br utfpr.edu.br)
domains_regexp = /@(?:#{domains.map { |d| Regexp.escape(d) }.join('|')})\z/
email = 'test.test@test.com'
Benchmark.ips do |x|
x.report('array') do
domains.include?(email.split('@')[1])
end
x.report('regexp') do
email =~ domains_regexp
end
x.compare!
end
@rubensmabueno
Copy link

👍 Merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment