查看文件中是否找到字符串的最快方法?
假设我有一个大文件。该文件是一个 IP 列表,每个 IP 都在新行上,如下所示:
123.123.123.123
123.123.123.124
123.123.123.125
etc...
我可以这样做:
$file = file_get_contents($src);
if (substr_count($file,'myip'))
echo 'FOUND';
或者使用数组样式的类似方式:
$file = file($src,FILE_IGNORE_NEW_LINES);
if (in_array('myip',$file))
echo 'FOUND';
但我认为还有第三种替代方案可以更快。
逐行解析文件,如果找到字符串,当然会停止读取。
事情是这样的:
$file = fopen($src,'r');
while(!feof($file)) {
$ip = fgets($file);
if ($ip == $myIP) {
die('Found');
}
}
fclose($file);
我的问题是:你认为还有其他更好的方法吗?
从性能角度来看,您认为哪种代码更快?
非常感谢大家
Let's say I have a big file. The file is a list of IPs each on a new line like this:
123.123.123.123
123.123.123.124
123.123.123.125
etc...
I could do it like this:
$file = file_get_contents($src);
if (substr_count($file,'myip'))
echo 'FOUND';
Or a similar way with array style:
$file = file($src,FILE_IGNORE_NEW_LINES);
if (in_array('myip',$file))
echo 'FOUND';
But I think there is a third alternative that could be faster.
Parsing the file line by line and of course stopping the reading if the string is found.
Something like this:
$file = fopen($src,'r');
while(!feof($file)) {
$ip = fgets($file);
if ($ip == $myIP) {
die('Found');
}
}
fclose($file);
My question is: do you think there are other better way?
And performance-wise which code do you think is faster?
Thanks a lot guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果可以,请考虑使用
grep
或查找
。它可能比 PHP 更快,更重要的是,更节省内存。If you can, consider using
grep
orfind
. It might be faster and, more importantly, more memory-efficient than PHP.假设需要用某种语言执行此操作,那么逐行修改代码可能是最好的选择,这样您就不会获取超出所需的数据。
Assuming doing this in a language is a requirement, a modification of your line-by-line code would probably be best, so you're not grabbing any more data than you need.
如果您可以保持文件排序,那么执行二分搜索效果会很好。
但是,对文件进行排序需要一些时间,因此如果您执行大量插入/删除操作,则可能不会很有效。
If you could keep the file sorted, then performing a binary search would perform well.
However, sorting the file will take some time, so if you are doing a lot of inserts/deletes it might not be very effective.