加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

是否可以加快PHP中的递归文件扫描?

发布时间:2020-12-13 14:13:19 所属栏目:PHP教程 来源:网络整理
导读:我一直试图在 PHP中复制 Gnu Find(“find”),但似乎不可能接近它的速度. PHP实现使用至少两次Find.有更快的方式使用PHP吗? 编辑:我添加了使用SPL实现的代码示例 – 其性能等于迭代方法 EDIT2:当从PHP调用find时,实际上比本地PHP实现的慢.我想我应该对我有
我一直试图在 PHP中复制 Gnu Find(“find”),但似乎不可能接近它的速度. PHP实现使用至少两次Find.有更快的方式使用PHP吗?

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

EDIT2:当从PHP调用find时,实际上比本地PHP实现的慢.我想我应该对我有什么满意:)

// 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 "$pathn";
      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 "$pathn";        
        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只是不能像C一样简单,简单.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读