有可能加快在PHP中的递归文件扫描? [英] Is it possible to speed up a recursive file scan in PHP?

查看:146
本文介绍了有可能加快在PHP中的递归文件扫描?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在PHP中复制 Gnu Find (find。) ,但似乎不可能接近速度。 PHP实现至少使用Find的两倍时间。有没有更快的方式来做到这一点与PHP?

编辑:我添加了一个代码示例使用SPL实现 - 其性能等于迭代方法



编辑2:从PHP调用find时,实际上比原生PHP实现慢。我想我应该对我得到的满意:)

  //当运行速度达到gnu find的317%时直接从shell 
函数list_recursive($ dir){
if($ dh = opendir($ dir)){
while(false!==($ entry = readdir($ dh) )){
if($ entry =='。'|| $ entry =='..')continue;

$ path =$ dir / $ entry;
回显$ path \\\
;
if(is_dir($ path))list_recursive($ path);
}
closedir($ d);



//直接从shell运行时,测量到gnu find的速度的315%
函数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;
回显$ path \\\
;
if(is_dir($ path))$ dirs [] = $ path;
}
closedir($ dh);



$ b //当从shell直接运行时,gnu find的速度达到315%
函数list_recursivedirectoryiterator($ path){
$ it = new RecursiveDirectoryIterator($ path);
foreach($ it as $ file){
if($ file-> isDot())continue;

echo $ file-> getPathname();



//当从shell直接运行时,测量到gnu find的速度的390%
函数list_gnufi​​nd($ dir){
$ dir = escapeshellcmd($ dir);
$ h = popen(/ usr / bin / find $ dir,r);
while(''!=($ s = fread($ h,2048))){
echo $ s;
}
pclose($ h);


解决方案

作为C,简单明了。

I've been trying to replicate Gnu Find ("find .") in PHP, but it seems impossible to get even close to its speed. The PHP implementations use at least twice the time of Find. Are there faster ways of doing this with PHP?

EDIT: I added a code example using the SPL implementation -- its performance is equal to the iterative approach

EDIT2: When calling find from PHP it was actually slower than the native PHP implementation. I guess I should be satisfied with what I've got :)

// 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);
}

解决方案

PHP just cannot perform as fast as C, plain and simple.

这篇关于有可能加快在PHP中的递归文件扫描?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆