He intentado replicar Gnu Find ("buscar") en PHP, pero parece imposible acercarse a su velocidad. Las implementaciones de PHP usan al menos el doble de tiempo que Find. ¿Hay formas más rápidas de hacerlo con PHP?¿Es posible acelerar un escaneo recursivo de archivos en PHP?
EDIT: He añadido un ejemplo de código usando la aplicación SPL - su rendimiento se corresponde con el enfoque iterativo
Edit2: Al llamar al hallazgo de PHP en realidad era más lenta que la aplicación nativa de PHP. Creo que debería estar satisfecho con lo que tengo :)
// measured to 317% of gnu find's speed when run directly from a shell
function list_recursive($dir) {
if ($dh = opendir($dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
$path = "$dir/$entry";
echo "$path\n";
if (is_dir($path)) list_recursive($path);
}
closedir($d);
}
}
// measured to 315% of gnu find's speed when run directly from a shell
function list_iterative($from) {
$dirs = array($from);
while (NULL !== ($dir = array_pop($dirs))) {
if ($dh = opendir($dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
$path = "$dir/$entry";
echo "$path\n";
if (is_dir($path)) $dirs[] = $path;
}
closedir($dh);
}
}
}
// measured to 315% of gnu find's speed when run directly from a shell
function list_recursivedirectoryiterator($path) {
$it = new RecursiveDirectoryIterator($path);
foreach ($it as $file) {
if ($file->isDot()) continue;
echo $file->getPathname();
}
}
// measured to 390% of gnu find's speed when run directly from a shell
function list_gnufind($dir) {
$dir = escapeshellcmd($dir);
$h = popen("/usr/bin/find $dir", "r");
while ('' != ($s = fread($h, 2048))) {
echo $s;
}
pclose($h);
}
Probablemente no. PHP es de alto nivel y probablemente nunca coincida con algo como Gnu Find (que probablemente está escrito en C) en términos de velocidad. Además, es notoriamente lento en la recursión. –
+1 por ejemplo código – buggedcom