Skip to content

Improve performance of checking domains#62

Merged
micke merged 1 commit intomicke:masterfrom
SoftSwiss:performance
Jan 16, 2017
Merged

Improve performance of checking domains#62
micke merged 1 commit intomicke:masterfrom
SoftSwiss:performance

Conversation

@Bishop
Copy link
Copy Markdown
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
Copy Markdown
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