2
\$\begingroup\$

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); 
 } 
}
rolfl
98.1k17 gold badges219 silver badges419 bronze badges
asked Jul 29, 2011 at 16:18
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Optimize how? Where is the bottleneck? What have your profiling results shown? \$\endgroup\$ Commented Jul 29, 2011 at 17:51
  • \$\begingroup\$ im not shire about foreach construction, and many if's \$\endgroup\$ Commented Jul 29, 2011 at 18:27
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 29, 2011 at 18:39

1 Answer 1

2
\$\begingroup\$

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

Then used Xdebug + CacheGrind to try different versions.

answered Jul 30, 2011 at 6:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.