Skip to content

Instantly share code, notes, and snippets.

@telephone
Created October 26, 2012 02:49
Show Gist options
  • Save telephone/3956622 to your computer and use it in GitHub Desktop.
Save telephone/3956622 to your computer and use it in GitHub Desktop.
File match regex benchmark for RET
<?php
/**
* Results:
*
* PHP 5.4: http://codepad.viper-7.com/vcV3z4
* PHP 5.3: http://codepad.viper-7.com/4FTY5n
* PHP 5.2: http://codepad.viper-7.com/O2cBmn
*
* Recommendation:
*
* Use Test #2. It's multi-OS compatible, it performs exact matches for
* extensions instead of lazy matches (like Test #3), and the offest between
* Test #2 vs Test #3 is minimal
*/
echo 'Regex benchmark for RET.', '<br><br>', 'Using 10,000 iterations to benchmark','<br><br>';
$test = array(
'/path/to/greedy.php',
'/path/to/home/.htaccess',
'/path/to/.htpasswd',
'/path/to/meta/.meta',
'/path/to/pizza/pizza.html',
'/path/to/food.jpg',
'/path/to/php/match.php',
'/path/to/github.php5',
'/path/to/ingredients/dough.htm'
);
/**
* Test #1 - Path with negative lookbehind/s
*
* @return
* filename
*/
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
foreach($test as $val) {
preg_match('#(?:.(?<!/))+(?<!\.htaccess|\.htpasswd|\.php|\.meta)$#i', $val, $matches);
if (!empty($matches)) {
//echo $matches[0], '<br>';
}
}
}
$end = microtime(true);
$time = $end - $start;
echo 'Test #1 - executed in: ', ($end - $start), ' seconds', '<br>';
/**
* Test #2 - Negative lookbehind using basename()
*
* @return
* filename
*/
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
foreach($test as $val) {
preg_match('#^(.*)(?<!\.htaccess|\.htpasswd|\.php|\.meta)$#i', basename($val), $matches);
if (!empty($matches)) {
//echo $matches[1], '<br>';
}
}
}
$end = microtime(true);
$time = $end - $start;
echo 'Test #2 - executed in: ', ($end - $start), ' seconds', '<br>';
/**
* Test #3 - Negative lookahead using basename()
*
* Quickest regex, but lazy matching has it's downsides:
* - using '\.htm' would exclude all .html files
*
* You can change this behaviour by adding negative lookaheads, but it's sloppy
* and I wouldn't recommend it:
* - to block .htm but allow .html files, '.*\.htm(?!l)'
*
* @return
* filename
*/
$start = microtime(true);
for ($i=0; $i < 10000; $i++) {
foreach($test as $val) {
preg_match('#^(?!\.htaccess|\.htpasswd|.*\.php|\.meta)(.*)$#i', basename($val), $matches);
if (!empty($matches)) {
//echo $matches[1], '<br>';
}
}
}
$end = microtime(true);
$time = $end - $start;
echo 'Test #3 - executed in: ', ($end - $start), ' seconds', '<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment