note 57114 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:02:15 +0000 |
| Subject: | note 57114 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126524@lists.php.net to get a copy of this message | ||
Note Submitter:
----
To beanz at gangstersonline dot co dot uk, just a little optimization tip.
You have make a lot of eregi calls in a sequence of elseif statements. That means that for a worst
case, you'll be making 8 eregi calls per iteration. Instead of using a bunch of conditional
eregi() calls (which is much slower compared to preg_match or strpos), you could do the following:
<?php
if ($i)
//create a mapping array
$ext_map = array('exe' => 'exe', 'bmp' => 'image1',
etc....)
foreach($tab as $rep) {
//use one preg_match to grab the extention
if (preg_match('/\.([a-z]+)$/', $rep, $matches) {
$ext = $ext_map[$matches[1]];
} else {
$ext = 'text';
}
//rest of code...
}
?>
So instead of a worst case of n eregi calls per iteration, you're guaranteed only one
preg_match per call. Or possibly even faster than preg_match:
$ext = $ext_map[substr($rep, strrpos($rep, '.'))];