1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
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
|