note 79396 added to function.glob
| From: | DavidfromCodeXplorer at osu1 dot php dot net | Date: | Sun, 25 Nov 2007 05:13:20 +0000 |
| Subject: | note 79396 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132551@lists.php.net to get a copy of this message | ||
After much testing I find that although glob() is much cleaner than opendir(), it is also much
slower.
I started by making a dir called "cache" in which I placed a single 65k html file filled
with whitespace (just so I could have a large filesize without worrying about char output).
Based on the code below
<?php
//Waste a little time to offset PHP's startup.
for($i=0; $i<20; $i++) {
print ' ';
}
$start_time = time()+microtime(true);
ob_start();
for($x=0; $x < 20; $x++) {
$dir = 'cache/';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//Include the file
include($file);
//$value = rand(0, 10000) * 2345678;
}
closedir($dh);
}
}
}
ob_end_clean();
print ((time()+microtime(true)) - $start_time). ' (opendir time)<br />';
$start_time = time()+microtime(true);
ob_start();
for($x=0; $x < 20; $x++) {
//Include the function files
foreach (glob("cache/*.html") as $file) {
//Include the file
include($file);
//$value = rand(0, 10000) * 2345678;
}
}
ob_end_clean();
print ((time()+microtime(true)) - $start_time). ' (glob time)<br />';
?>
The result was:
0.0069890022277832 (opendir time)
0.027945041656494 (glob time)
So I can honestly recommend opendir if you plan on including the files in the dir.
----
Server IP: 69.147.83.197
Probable Submitter: 208.101.216.113
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- https://master.php.net/note/edit/79396
Del: integrated -- https://master.php.net/note/delete/79396/integrated
Del: useless -- https://master.php.net/note/delete/79396/useless
Del: bad code -- https://master.php.net/note/delete/79396/bad+code
Del: spam -- https://master.php.net/note/delete/79396/spam
Del: non-english -- https://master.php.net/note/delete/79396/non-english
Del: in docs -- https://master.php.net/note/delete/79396/in+docs
Del: other reasons-- https://master.php.net/note/delete/79396
Reject -- https://master.php.net/note/reject/79396
Search -- https://master.php.net/manage/user-notes.php