脚本宝典收集整理的这篇文章主要介绍了是否可以加快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 "$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中的递归文件扫描?全部内容,希望文章能够帮你解决是否可以加快PHP中的递归文件扫描?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。