该脚本必须验证一个预定义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