Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pathinfo vs fnmatch

There was a small debate regarding the speed of fnmatch over pathinfo here : 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?

EDIT: Using mac php from cmd

PHP 5.3.0 (cli) (built: Jul 20 2009 13:56:33) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

dynamic path pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05

static path pathinfo 0.86487698554993 fnmatch 1.0420439243317 x1.2

mac xampp php from cmd

PHP 5.3.1 (cli) (built: Feb 27 2010 12:41:51) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

dynamic path pathinfo 3.63922715187 fnmatch 4.99041700363 x1.37

static path pathinfo 1.03110480309 fnmatch 2.38929820061 x2.32

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>
like image 661
zaf Avatar asked Apr 22 '10 18:04

zaf


2 Answers

My results are opposite of yours:

php -f 2693428.php
dynamic path
pathinfo 4.5834331512451
fnmatch 3.2174317836761
x0.7

static path
pathinfo 2.1787130832672
fnmatch 0.95714497566223
x0.44

Version

PHP 5.3.0 (cli) (built: Jun 29 2009 21:25:23)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
like image 157
OIS Avatar answered Sep 30 '22 23:09

OIS


Using the same benchmarking code

dynamic path
pathinfo 2.6542711257935
fnmatch 1.9943950176239
x0.75

static path
pathinfo 1.1711349487305
fnmatch 0.54186105728149
x0.46

PHP Version 5.3.1
Build Date  Nov 20 2009 17:20:57
Compiler    MSVC6 (Visual C++ 6.0)
Architecture    x86
Thread Safety   enabled
Zend Memory Manager enabled
Zend Multibyte Support  disabled
like image 33
Ben Avatar answered Sep 30 '22 21:09

Ben