ID:               31515
 User updated by:  akorthaus at web dot de
 Reported By:      akorthaus at web dot de
-Status:           Feedback
+Status:           Open
 Bug Type:         Performance problem
 Operating System: Linux 2.4.28 (Gentoo)
 PHP Version:      5.0.3
 New Comment:

With a small directory I get:

my_scandir:
count: 71
1.63159513474

scandir:
count: 71
3.12091302872

With 100.000 files it takes too long, and scandir() runs into
memory_limit (which is 500 MB!)

scandir() seems to need much more memory!
I added the following line to the scripts:
echo "mem: ".number_format(memory_get_usage()/1048576) . "M\n";

so I get:

my_scandir:
mem: 10M
count: 100002
1.38867807388

scandir:
mem: 397M
count: 100002
1.75003910065


If I put in (scandir version):

foreach(range(1, 2) as $unused)

I get:

Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to
allocate 4096 bytes) in /home/akorthaus/test/scan.php on line 5


If I put in (my_scandir version):

foreach(range(1, 10) as $unused)

mem: 10M
count: 100002
16.7273759842

which is the same as with only one cycle.


Previous Comments:
------------------------------------------------------------------------

[2005-01-12 21:51:42] [EMAIL PROTECTED]

count: 2034                  
251.505897045                
                             
count: 2034                  
155.706785917                

Only difference:
foreach(range(1, 5000) as $unused)
    $files = scandir('C:\WINDOWS\System32');

So, not on Win32. Do a foreach like I have done and spread the function
call over quite a few calls, because with repeated execution of a single
function call, it went back and forth for me.

------------------------------------------------------------------------

[2005-01-12 13:48:34] akorthaus at web dot de

Description:
------------
I do not understand why the new scandir() function is slower than an
own PHP-function which does the same (I used the "Example 2. PHP 4
alternatives to scandir()" from manual).

I tried this with 50 - 100.000 files, but the result is allways the
same. 

my_scandir() is about 50%-100% faster. If I don't sort, it is about
400% faster.

Reproduce code:
---------------
<?php
function my_scandir($dir) {
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
    }
    sort($files);
    return $files;
}
$t1= microtime(TRUE);
$files = my_scandir('/tmp');
$t2= microtime(TRUE);
echo "count: ".count($files)."\n";
echo $t2-$t1;
echo "\n";
?>

<?php
$t1 = microtime(TRUE);
$files = scandir('/tmp');
$t2= microtime(TRUE);
echo "count: ".count($files)."\n";
echo $t2-$t1;
echo "\n";
?>

Expected result:
----------------
I expect the c-function to be faster

Actual result:
--------------
the php-function is about 50-100% faster


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=31515&edit=1

Reply via email to