From: nangnahz at gmail dot com
Operating system: Ubuntu 7.04
PHP version: 5.2.4
PHP Bug Type: Performance problem
Bug description: scandir() is much more slower than readdir()
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......
--
Edit bug report at http://bugs.php.net/?id=42679&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=42679&r=trysnapshot44
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=42679&r=trysnapshot52
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=42679&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=42679&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=42679&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=42679&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=42679&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=42679&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=42679&r=support
Expected behavior: http://bugs.php.net/fix.php?id=42679&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=42679&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=42679&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=42679&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=42679&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=42679&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=42679&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=42679&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=42679&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=42679&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=42679&r=mysqlcfg