pathinfo vs fnmatch [英] pathinfo vs fnmatch

查看:75
本文介绍了pathinfo vs fnmatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Using dynamic and static paths showed that pathinfo was faster.

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

Is my benchmarking logic and conclusion valid?

编辑:使用来自cmd的mac php

EDIT: Using mac php from cmd

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 技术

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

动态路径pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05

dynamic path pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05

静态路径pathinfo 0.86487698554993 fnmatch 1.0420439243317 x1.2

static path pathinfo 0.86487698554993 fnmatch 1.0420439243317 x1.2

来自cmd的mac xampp php

mac xampp php from cmd

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 技术

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

动态路径pathinfo 3.63922715187 fnmatch 4.99041700363 x1.37

dynamic path pathinfo 3.63922715187 fnmatch 4.99041700363 x1.37

静态路径pathinfo 1.03110480309 fnmatch 2.38929820061 x2.32

static path pathinfo 1.03110480309 fnmatch 2.38929820061 x2.32

我提供了一个结果示例,该示例在我的计算机上进行了100,000次迭代以秒为单位:

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

代码:

<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>

推荐答案

我赞成所有答案,但会回答我自己的问题.

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!

这是第2部分的问题:

here is the part 2 question: (Pathinfo vs fnmatch part 2) Speed benchmark reversed on Windows and Mac

这篇关于pathinfo vs fnmatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆