pathinfo vs fnmatch

zaf*_*zaf 7 php benchmarking pathinfo fnmatch

关于fnmatch在pathinfo上的速度有一个小小的争论:如何检查文件是否是php?

我并不完全相信所以决定对两个功能进行基准测试.

使用动态和静态路径显示pathinfo更快.

我的基准逻辑和结论是否有效?

编辑:从cmd使用mac php

PHP 5.3.0(cli)(内置:2009年7月20日13:56:33)版权所有(c)1997-2009 PHP Group Zend Engine v2.3.0,版权所有(c)1998-2009 Zend Technologies

动态路径pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05

static path pathinfo 0.86487698554993 fnmatch 1.0420439243317 x1.2

来自cmd的mac xampp php

PHP 5.3.1(cli)(内置:2010年2月27日12:41:51)版权所有(c)1997-2009 PHP Group Zend Engine v2.3.0,版权所有(c)1998-2009 Zend Technologies

动态路径pathinfo 3.63922715187 fnmatch 4.99041700363 x1.37

static path pathinfo 1.03110480309 fnmatch 2.38929820061 x2.32

我在我的机器上包含一个结果样本,以秒为单位进行100,000次迭代:

dynamic path
pathinfo 3.79311800003
fnmatch 5.10071492195
x1.34

static path
pathinfo 1.03921294212
fnmatch 2.37709188461
x2.29
Run Code Online (Sandbox Code Playgroud)

码:

<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>
Run Code Online (Sandbox Code Playgroud)

zaf*_*zaf 1

我对所有答案都投了赞成票,但会回答我自己的问题。

我的基准测试逻辑和结论是有效的,并且所有答案基准都是有效的。

我找到了原因,这提出了另一个问题,而是让这篇文章偏离主题并使其更长,我将提出另一个问题。完成后我会将新链接放在这里。

感谢您为我进行基准测试!

编辑:这是第 2 部分的问题:(Pathinfo 与 fnmatch 第 2 部分)Windows 和 Mac 上的速度基准相反