Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm trying to optimize performance for recursive directory listing with some exceptions:

function listdir($basedir) {
    global $root, $ignore_dirs, $ignore_files, $filetypes, $ignore_starts_with;
        if ($handle = @opendir($basedir)) { 
            while (false !== ($fn = readdir($handle))) { 
                if ($fn != '.' && $fn != '..') {

                    $s = true;
                    foreach ($ignore_starts_with as $pre) if (strpos($fn, $pre) === 0) $s = false;

                    $dir = $basedir."/".$fn;
                    if ($s && is_dir($dir) && !in_array($fn, $ignore_dirs)) {
                        listdir($dir);
                    } else {

                        if ($s && !in_array($fn,$ignore_files) && preg_match("/[^.\/].+\.($filetypes)$/",$dir,$fname)) printlink($fname[0]);
                    } 
                } 
            } 
        closedir($handle); 
    } 
}
share|improve this question
3  
Optimize how? Where is the bottleneck? What have your profiling results shown? –  Ed S. Jul 29 '11 at 17:51
    
im not shire about foreach construction, and many if's –  350D Jul 29 '11 at 18:27
3  
Well, I would prove that is actually a bottleneck before potentially wasting time optimizing them out (if that's even possible). For example, if 98% of your time is spent doing I/O then removing an if statement or a loop is pointless. That said, your $s variable will only hold the last assignment, so you may as well just break out of it the first time that condition is met. –  Ed S. Jul 29 '11 at 18:39

1 Answer 1

First of all, I'd use RecursiveDirectoryIterator, removed globals and corrected formatting.

Then used Xdebug + CacheGrind to try different versions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.