我必须找到文件夹中“最深”文件夹的路径。为此,我实现了两种算法,一种比另一种算法快。 有谁知道为什么?我想这与硬盘硬件有一些联系,但我想明白。 这里是一个快:通过深度优先或宽度优先发现文件夹树
private function getHostAux($path) {
$matches = array();
$folder = rtrim($path, DIRECTORY_SEPARATOR);
$moreFolders = glob($folder.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
if (count($moreFolders) == 0) {
$matches[] = $folder;
} else {
foreach ($moreFolders as $fd) {
$arr = $this->getHostAux($fd);
$matches = array_merge($matches, $arr);
}
}
return $matches;
}
这里是缓慢的一个:
/**
* Breadth-first function using glob
*/
private function getHostAux($path) {
$matches = array();
$folders = array(rtrim($path, DIRECTORY_SEPARATOR));
$i = 0;
while($folder = array_shift($folders)) {
$moreFolders = glob($folder.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
if (count($moreFolders == 0)) {
$matches[$i] = $folder;
}
$folders = array_merge($folders, $moreFolders);
$i++;
}
return $matches;
}
谢谢!
“慢”和“快”是什么意思?你如何衡量它?你有多少次调用每种方法来防止第一次读取速度减慢? – andr