note 79965 added to function.glob
| From: | ob dot phpfromdaevel dot fr at osu1 dot php dot net | Date: | Fri, 21 Dec 2007 10:36:14 +0000 |
| Subject: | note 79965 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133842@lists.php.net to get a copy of this message | ||
I don't agree with "David from CodeXplorer" 's benchmark :
The "include()" function is much more slower than the opendir/glob function, and in the
case of the "opendir" check you haven't correct the filename : so the include does
not work.
Here (I think) a more accuracy benchmark :
<?php
error_reporting( E_ALL );
$nb_loops = 100000 ;
function time_exact()
{
$tmp = explode( ' ', microtime() );
return $tmp[1] . substr( $tmp[0], 1 );
}
clearstatcache();
$start_time = time_exact();
for( $i = 0; $i < $nb_loops; $i++ )
{
$path = 'cache';
if( $dir = opendir( $path ) )
{
while( ( $file = readdir( $dir ) ) !== false )
{
if( preg_match( '#^.*\\.html$#S', $file ) )
{
$correct_file_path = $path .'/'. $file;
// Ok, file found... do what you want here
// $for_example = file_get_contents( $correct_file_path );
}
}
closedir( $dir );
}
}
echo round( 1000 * bcsub( time_exact(), $start_time, 6 ), 3 ), 'ms - opendir', PHP_EOL;
clearstatcache();
$start_time = time_exact();
for( $i = 0; $i < $nb_loops; $i++ )
{
$list = glob( 'cache/*.html', GLOB_NOSORT );
foreach( $list as $correct_file_path )
{
// Ok, file found... do what you want here
// $for_example = file_get_contents( $correct_file_path );
}
}
echo round( 1000 * bcsub( time_exact(), $start_time, 6 ), 3 ), 'ms - glob', PHP_EOL;
?>
The test directory contains 4 files :
-rw-r--r-- 1 dev-olivier www-data 0 2007-12-21 10:52 another_file_ok.html
-rw-r--r-- 1 dev-olivier www-data 0 2007-12-21 10:52 file_ok.html
-rw-r--r-- 1 dev-olivier www-data 0 2007-12-21 10:52 .htaccess
-rw-r--r-- 1 dev-olivier www-data 0 2007-12-21 10:52 incorrect_file.jpg
And results :
dev-olivier@debian:~/test$ php bench.php
3277.041ms - opendir
2423.365ms - glob
dev-olivier@debian:~/test$ php bench.php
3253.049ms - opendir
2423.135ms - glob
dev-olivier@debian:~/test$ php bench.php
3282.284ms - opendir
2420.401ms - glob
So : glop is of course faster than opendir/readdir/closedir, and is simpler to use.
But I think opendir is much more "powerfull", especially for recursive jobs.
PS : you can check with or without the sort feature of glob(), or with the readfile(), results as
same.
----
Server IP: 217.174.196.197
Probable Submitter: 89.159.30.121
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- https://master.php.net/note/edit/79965
Del: integrated -- https://master.php.net/note/delete/79965/integrated
Del: useless -- https://master.php.net/note/delete/79965/useless
Del: bad code -- https://master.php.net/note/delete/79965/bad+code
Del: spam -- https://master.php.net/note/delete/79965/spam
Del: non-english -- https://master.php.net/note/delete/79965/non-english
Del: in docs -- https://master.php.net/note/delete/79965/in+docs
Del: other reasons-- https://master.php.net/note/delete/79965
Reject -- https://master.php.net/note/reject/79965
Search -- https://master.php.net/manage/user-notes.php