Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

APDZ Tarnic Photoshop Lab Capital Tutorials 

Change Language

Who's online

There are currently 0 users and 31 guests online.

glob vs opendir

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.


Submitted by David on November 25, 2007 - 5:17am.
printer friendly version

Explode vs simple Regex

After further testing I found that a simple preg_match() search is also faster than exploding something.

0.0014879703521729 explode
0.0012550354003906 preg
0.054396152496338 glob

<?php

for($i=0$i<20$i++) { 
    print 
' '
}
$start_time time()+microtime(true);

ob_start();
for(
$x=0$x 10$x++) {
   
//glob() is much slower so we use opendir...
   
foreach(array('cache/''cache/') as $dir) {
        if (
$dh opendir($dir)) {
            while ((
$file readdir($dh)) !== false) {
                if(
end(explode('.'$file)) == 'php') {
                   
//Include the file 
                   
require_once($dir$file);
                   
//Update the array of files included
                   
$site_config["included_files"][$file] = filesize($file);
                } else {
                    print 
$dir' '$file'<br />'
                }
            }
           
closedir($dh);
        } else {
            die(
'<h2>Error!</h2<p>Couldn\'t load '$dir'</p>');
        }
    }
}
ob_end_clean();

print ((
time()+microtime(true)) - $start_time). ' explode <br />';





$start_time time()+microtime(true);

ob_start();
for(
$x=0$x 20$x++) {
   
//glob() is much slower so we use opendir...
   
foreach(array('cache/''cache/') as $dir) {
        if (
$dh opendir($dir)) {
            while ((
$file readdir($dh)) !== false) {
                if(
preg_match("/.php/"$file)) {
                   
//Include the file 
                   
require_once($dir$file);
                   
//Update the array of files included
                   
$site_config["included_files"][$file] = filesize($file);
                } else {
                    print 
$dir' '$file'<br />'
                }
            }
           
closedir($dh);
        } else {
            die(
'<h2>Error!</h2<p>Couldn\'t load '$dir'</p>');
        }
    }
}
ob_end_clean();

print ((
time()+microtime(true)) - $start_time). ' preg <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 <br />';
?>


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati