readdir与scandir [英] readdir vs scandir
问题描述
1]哪个功能更快?
2]有什么区别?
1] Which of the functions is faster?
2] what are the differences?
Differences
1] readdir返回目录中下一个条目的名称. Scandir从目录返回文件和目录的数组.
1] readdir returns the name of the next entry in the directory. Scandir returns an array of files and directories from the directory.
2] readdir必须打开资源句柄,直到读取所有条目. scandir,也许创建了所有条目的数组并关闭了资源句柄?
2] readdir has to have a resource handle open until all the entries are read. scandir, perhaps creates an array of all the entries and closes the resouce handle?
推荐答案
仅获得结果(不做任何事情),readdir的速度至少要快:
Just getting the results (without doing anything), readdir is a minimum faster:
<?php
$count = 10000;
$dir = '/home/brati';
$startScan = microtime(true);
for ($i=0;$i<$count;$i++) {
$array = scandir($dir);
}
$endScan = microtime(true);
$startRead = microtime(true);
for ($i=0;$i<$count;$i++) {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
// We do not know what to do
}
}
$endRead = microtime(true);
echo "scandir: " . ($endScan-$startScan) . "\n";
echo "readdir: " . ($endRead-$startRead) . "\n";
赠予:
== RUN 1 ==
scandir: 5.3707950115204
readdir: 5.006147146225
== RUN 2 ==
scandir: 5.4619920253754
readdir: 4.9940950870514
== RUN 3 ==
scandir: 5.5265231132507
readdir: 5.1714680194855
然后,这当然取决于您打算做什么.如果您必须使用scandir()编写另一个循环,则会更慢.
Then of course it depends on what you intend to do. If you have to write another loop with scandir(), it will be slower.
这篇关于readdir与scandir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!