A little something I whipped up to avoid my zoology homework (I /really/ don't
care that the damn sperm of nematodes are unusual because they lack a
flagellum and acrosome). Conclusion: substr is faster than preg_match
[tadpole tadpole]$ php -q ./bench.php
substr: 3.6424000263214
PCRE: 5.121386051178
substr: 3.2655299901962
PCRE: 3.8099709749222
substr: 3.2664449214935
PCRE: 3.7604590654373
substr: 3.2712109088898
PCRE: 3.7643429040909
substr: 3.496111035347
PCRE: 4.4902020692825
substr: 3.3643230199814
PCRE: 4.404403090477
substr: 3.5383290052414
PCRE: 4.0583729743958
substr: 3.3862169981003
PCRE: 5.0897340774536
substr: 4.3838140964508
PCRE: 3.887619972229
substr: 3.4568190574646
PCRE: 3.8514100313187
Averages:
substr: 3.5071199059486
pcre: 4.2237901210785
[tadpole tadpole]$ cat ./bench.php
<?php
function getmicrotime() {
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
$pcre = array();
$substr = array();
for ( $x=0 ; $x<10 ; $x++ ) {
$array = array();
for ( $y=0 ; $y<100000 ; $y++ ) {
array_push($array, mt_rand().'.thumb.jpg');
}
$results = array();
$start = getmicrotime();
foreach ( $array as $a ) {
if ( substr($a, -10) == '.thumb.jpg' )
array_push($results, $a);
}
$substr[$x] = (getmicrotime()-$start);
echo "substr: {$substr[$x]}\n";
unset($results);
$results = array();
$start = getmicrotime();
foreach ( $array as $a ) {
if ( preg_match('/\.thumb\.jpg$/', $a) )
array_push($results, $a);
}
$pcre[$x] = (getmicrotime()-$start);
echo "PCRE: {$pcre[$x]}\n";
unset($results, $array);
}
echo "Averages:\n";
echo "substr: ".($avg['substr']=(array_sum($substr)/count($substr)))."\n";
echo "pcre: ".($avg['pcre']=(array_sum($pcre)/count($pcre)))."\n";
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php