Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of checking domains #62

Merged
merged 1 commit into from
Jan 16, 2017
Merged

Conversation

Bishop
Copy link
Contributor

@Bishop Bishop commented Jan 16, 2017

I need to analyse thousands of domains and it takes much time.

Some experiments:

    def domain_is_in?(domain_list)
      domain_list.select { |domain|
        address.domain =~ (/\A(.*\.)*#{domain}\z/i)
      }.any?
    end

    def domain_is_in_2?(domain_list)
      domain_list.any? { |domain|
        address.domain =~ /\A(.*\.)*#{domain}\z/i
      }
    end

    def domain_is_in_3?(domain_list)
      address_domain = address.domain.downcase
      domain_list.any? { |domain|
        address_domain =~ /\A(?:.+\.)*?#{domain}\z/
      }
    end

    def domain_is_in_4?(domain_list)
      address_domain = address.domain.downcase
      domain_list.any? { |domain|
        address_domain.end_with?(domain) && address_domain =~ /\A(?:.+\.)*?#{domain}\z/
      }
    end

Code:

a = ValidEmail2::Address.new 'alex...@gmail.com'

n = 100
Benchmark.bm do |x|
  x.report { n.times do ; a.disposable?; end }
  x.report { n.times do ; a.disposable2?; end }
  x.report { n.times do ; a.disposable3?; end }
  x.report { n.times do ; a.disposable4?; end }
end

Results:

       user     system      total        real
   3.520000   0.090000   3.610000 (  3.628667)
   3.450000   0.050000   3.500000 (  3.499219)
   2.460000   0.040000   2.500000 (  2.511812)
   0.020000   0.000000   0.020000 (  0.023849)

It looks like 100x improvement.

@micke micke merged commit dfe68c9 into micke:master Jan 16, 2017
@micke
Copy link
Owner

micke commented Jan 16, 2017

Thanks!

@Bishop Bishop deleted the performance branch January 16, 2017 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants