【发布时间】:2011-02-11 05:08:11
【问题描述】:
关于 fnmatch 对 pathinfo 的速度有一个小争论:how to check if file is 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 技术
动态路径pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05
静态路径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 技术
动态路径pathinfo 3.63922715187 fnmatch 4.99041700363 x1.37
静态路径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
代码:
<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>
【问题讨论】:
-
应该是 CW,因为我猜这不是一个真正的问题
-
这是一个真正的问题!为什么其他人会得出不同的结论?
-
您运行了多少次测试?在多任务机器上,某些任务可能会延迟,从而减慢通常可能更快的部分代码。
-
我已经运行了很多次并且得到了相同的结果...我已经用命令行的结果更新了我的帖子。
标签: php benchmarking pathinfo fnmatch