路径信息与 fnmatch
关于 fnmatch 相对于路径信息的速度有一个小争论:如何检查是否文件是 php?
我并不完全相信,所以决定对这两个函数进行基准测试。
使用动态和静态路径表明 pathinfo 更快。
我的基准逻辑和结论有效吗?
编辑:从 cmd 使用 mac php
PHP 5.3.0 (cli)(构建日期:2009 年 7 月 20 日) 13:56:33) 版权所有 (c) 1997-2009 PHP 组 Zend 引擎 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 组 Zend 引擎 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>
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
Technologiesdynamic path pathinfo 3.2973630428314
fnmatch 3.4520659446716 x1.05static path pathinfo 0.86487698554993
fnmatch 1.0420439243317 x1.2mac 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
Technologiesdynamic path pathinfo 3.63922715187
fnmatch 4.99041700363 x1.37static 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的结果与你的相反:
版本
My results are opposite of yours:
Version
使用相同的基准测试代码
Using the same benchmarking code
运行你的
当
这四个,
像这样运行
并在我的机器上从 Zend Studio 进行分析时,
我发现这有点烦人,没有 4 是最快的,但这就是它所说的。每次调用的时间为 0.00000n 秒,无论如何也不用担心。
Running yours with
gives
These four
when run like this
and profiled on my machine from Zend Studio give
I find it somewhat annoying that no 4 is fastest, but that's what it says. And with 0.00000n seconds per call it's nothing to bother anyway.
我将把我的结果放在这里:
但请记住我对您链接到的问题的原始评论:
特定于特定扩展上的分支。
I'll throw my results in here:
But keep in mind my original comment on the question you linked to:
Specific to branching on a particular extension.
我对所有答案都投了赞成票,但会回答我自己的问题。
我的基准测试逻辑和结论是有效的,并且所有答案基准都是有效的。
我找到了原因,这提出了另一个问题,而是让这篇文章偏离主题并使其更长,我将提出另一个问题。完成后我会将新链接放在这里。
感谢您为我进行基准测试!
编辑:这是第二部分问题: (Pathinfo 与 fnmatch 第 2 部分)Windows 和 Mac 上的速度基准相反
I've upvoted all the answers but will answer my own question.
My benchmarking logic and conclusion is valid AND all the answers benchmarks are valid.
I've found the reason why, which has raised another question but rather making this post go off in a tangent and making it even longer I'll be opening another question. I'll put the new link here when I've done so.
Thanks for benchmarking for me!
EDIT: here is the part 2 question: (Pathinfo vs fnmatch part 2) Speed benchmark reversed on Windows and Mac