Sha256: 3c3a280fd4707e71036d3464c5287424fc703a7e6adc19b3a543ecfa600686ee

Contents?: true

Size: 1.65 KB

Versions: 180

Compression:

Stored size: 1.65 KB

Contents

require 'benchmark/ips'

STRING = "www.subdomain.example.com"
ARRAY  = %w(
  com
  example.com
  subdomain.example.com
  www.subdomain.example.com
)

def tokenizer1(string)
  parts = string.split(".").reverse!
  index = 0
  query = parts[index]
  names = []

  loop do
    names << query

    index += 1
    break if index >= parts.size
    query = parts[index] + "." + query
  end
  names
end

def tokenizer2(string)
  parts = string.split(".")
  index = parts.size - 1
  query = parts[index]
  names = []

  loop do
    names << query

    index -= 1
    break if index < 0
    query = parts[index] + "." + query
  end
  names
end

def tokenizer3(string)
  isx = string.size
  idx = string.size - 1
  names = []

  loop do
    isx = string.rindex(".", isx - 1) || -1
    names << string[isx + 1, idx - isx]

    break if isx <= 0
  end
  names
end

def tokenizer4(string)
  isx = string.size
  idx = string.size - 1
  names = []

  loop do
    isx = string.rindex(".", isx - 1) || -1
    names << string[(isx+1)..idx]

    break if isx <= 0
  end
  names
end

(x = tokenizer1(STRING)) == ARRAY or fail("tokenizer1 failed: #{x.inspect}")
(x = tokenizer2(STRING)) == ARRAY or fail("tokenizer2 failed: #{x.inspect}")
(x = tokenizer3(STRING)) == ARRAY or fail("tokenizer3 failed: #{x.inspect}")
(x = tokenizer4(STRING)) == ARRAY or fail("tokenizer4 failed: #{x.inspect}")

Benchmark.ips do |x|
  x.report("tokenizer1") do
    tokenizer1(STRING).is_a?(Array)
  end
  x.report("tokenizer2") do
    tokenizer2(STRING).is_a?(Array)
  end
  x.report("tokenizer3") do
    tokenizer3(STRING).is_a?(Array)
  end
  x.report("tokenizer4") do
    tokenizer4(STRING).is_a?(Array)
  end

  x.compare!
end

Version data entries

180 entries across 162 versions & 20 rubygems

Version Path
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/public_suffix-5.0.3/test/benchmarks/bm_names.rb
public_suffix-5.0.3 test/benchmarks/bm_names.rb
cloudsmith-api-2.0.7 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.6 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.5 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.4 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.3 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.2 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
postfinancecheckout-ruby-sdk-3.3.0 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
wallee-ruby-sdk-3.3.0 vendor/bundle/ruby/2.7.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.1 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
cloudsmith-api-2.0.0 vendor/bundle/ruby/2.6.0/gems/public_suffix-5.0.1/test/benchmarks/bm_names.rb
public_suffix-5.0.1 test/benchmarks/bm_names.rb