php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42679 scandir() is much more slower than readdir()
Submitted: 2007-09-15 22:45 UTC Modified: 2007-09-16 14:39 UTC
From: nangnahz at gmail dot com Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.2.4 OS: Ubuntu 7.04
Private report: No CVE-ID: None
 [2007-09-15 22:45 UTC] nangnahz at gmail dot com
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......

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-16 14:39 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

scandir() does a lot more then readdir
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC