該腳本必須驗證一個預定義IP是否存在於大量IP中。目前,該功能類似於這樣我的代碼(認爲「IPS」是我的IP的陣列和「IP」是預定義的IP)找到字符串數組中字符串的最快方法
ips.each do |existsip|
if ip == existsip
puts "ip exists"
return 1
end
end
puts "ip doesn't exist"
return nil
有沒有更快的方式做同樣的事情?
編輯:我可能錯誤地表達了我自己。我可以做array.include?但我想知道的是:array.include?該方法會給我最快的結果?
使用散列或代替將數組的 – Phrogz 2012-02-16 15:28:37
任何Ruby編程之前,請先閱讀http://ruby-doc.org/core-1.9.3/Enumerable.html。 – tokland 2012-02-16 15:30:17
你可以使用'Array'類中定義的'include?'方法使這個操作看起來更整潔,我不確定它是否會提高查找速度 – 2012-02-16 15:30:54