我必須找到文件夾中「最深」文件夾的路徑。爲此,我實現了兩種算法,一種比另一種算法快。 有誰知道爲什麼?我想這與硬盤硬件有一些聯繫,但我想明白。 這裏是一個快:通過深度優先或寬度優先發現文件夾樹
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