Es posible acelerar un escaneo recursivo de archivos en PHP?

He estado tratando de replicarGnu Find ("find.") en PHP, pero parece imposible acercarse incluso a su velocidad. Las implementaciones de PHP usan al menos dos veces el tiempo de Find. ¿Hay formas más rápidas de hacer esto con PHP?

EDIT: agregué un ejemplo de código usando la implementación SPL: su rendimiento es igual al enfoque iterativo

EDIT2: Al llamar a find desde PHP, en realidad era más lento que la implementación nativa de PHP. Supongo 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);
}

Respuestas a la pregunta(7)

Su respuesta a la pregunta