pathinfo vs fnmatch

Posted by zaf on Stack Overflow See other posts from Stack Overflow or by zaf
Published on 2010-04-22T18:31:23Z Indexed on 2010/04/22 18:33 UTC
Read the original article Hit count: 378

Filed under:
|

There was a small debate regarding the speed of fnmatch over pathinfo here : http://stackoverflow.com/questions/2692536/how-to-check-if-file-is-php

I wasn't totally convinced so decided to benchmark the two functions.

Using dynamic and static paths showed that pathinfo was faster.

Is my benchmarking logic and conclusion valid?

I include a sample of the results which are in seconds for 100,000 iterations on my machine :

dynamic path
pathinfo 3.79311800003
fnmatch 5.10071492195
x1.34

static path
pathinfo 1.03921294212
fnmatch 2.37709188461
x2.29

Code:

<pre>
<?php

$iterations=100000;

// Benchmark with dynamic file path
print("dynamic path\n");

$i=$iterations;
$t1=microtime(true);
while($i-->0){
    $f='/'.uniqid().'/'.uniqid().'/'.uniqid().'/'.uniqid().'.php';
    if(pathinfo($f,PATHINFO_EXTENSION)=='php') $d=uniqid();
}
$t2=microtime(true) - $t1;

print("pathinfo $t2\n");

$i=$iterations;
$t1=microtime(true);
while($i-->0){
    $f='/'.uniqid().'/'.uniqid().'/'.uniqid().'/'.uniqid().'.php';
    if(fnmatch('*.php',$f)) $d=uniqid();
}
$t3 = microtime(true) - $t1;

print("fnmatch $t3\n");

print('x'.round($t3/$t2,2)."\n\n");

// Benchmark with static file path
print("static path\n");

$f='/'.uniqid().'/'.uniqid().'/'.uniqid().'/'.uniqid().'.php';

$i=$iterations;
$t1=microtime(true);
while($i-->0) if(pathinfo($f,PATHINFO_EXTENSION)=='php') $d=uniqid();
$t2=microtime(true) - $t1;

print("pathinfo $t2\n");

$i=$iterations;
$t1=microtime(true);
while($i-->0) if(fnmatch('*.php',$f)) $d=uniqid();
$t3=microtime(true) - $t1;

print("fnmatch $t3\n");

print('x'.round($t3/$t2,2)."\n\n");

?>
</pre>

© Stack Overflow or respective owner

Related posts about php

Related posts about benchmarking