关于fnmatch在pathinfo上的速度,这里有一个小争论:how to check if file is php?
我并不完全相信,所以决定对这两个功能进行基准测试。
使用动态和静态路径显示pathinfo更快。
我的基准测试逻辑和结论是否有效?
我在我的机器上包含了一个结果示例,该示例以秒为单位进行了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>
最佳答案
我的结果与您相反:
php -f 2693428.php
dynamic path
pathinfo 4.5834331512451
fnmatch 3.2174317836761
x0.7
static path
pathinfo 2.1787130832672
fnmatch 0.95714497566223
x0.44
版本
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