|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-09-16 14:39 UTC] iliaa@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 03:00:01 2025 UTC |
Description: ------------ I try to traversal a directory. Include about 20000 files and 200 directories. Use scandir function, it cost about 70 seconds. Then I use readdir function, it cost only 6 seconds! System Version: Linux ubuntu 2.6.20-16-generic Apache/2.2.6 PHP/5.2.4 Mysql/5.0.45 Reproduce code: --------------- <?php function useScandir($dir) { $i = 0; while ($dir) { foreach (scandir($dir[0]) as $filename) { if ($filename != '.' && $filename != '..') { $filename = $dir[0] . '/' . $filename; if (is_dir($filename)) { echo $i . " [dir] " . $filename . "<br>"; $dir[] = $filename; } else { echo $i . " file: " . $filename . "<br>"; } $i++; } } array_shift($dir); } } useScandir(array ("/data/www/manual")); ?> <?php function useReaddir($dir) { $i = 0; while ($dir) { $dh = opendir($dir[0]); while (false !== ($filename = readdir($dh))) { if ($filename != '.' && $filename != '..') { $filename = $dir[0] . "/" . $filename; if (is_dir($filename)) { echo $i . " [dir] " . $filename . "<br>"; $dir[] = $filename; } else { echo $i . " file: " . $filename . "<br>"; } $i++; } } closedir($dh); array_shift($dir); } } useReaddir(array ('/data/www/manual')); ?> Expected result: ---------------- run fast!!! Actual result: -------------- it's very slow......