php - 路径信息与 fnmatch

标签 php benchmarking pathinfo fnmatch

这里有一个关于 fnmatch 相对于 pathinfo 的速度的小争论:how to check if file is php?

我并不完全相信,所以决定对这两个函数进行基准测试。

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

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

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 Technologies

dynamic path pathinfo 3.2973630428314 fnmatch 3.4520659446716 x1.05

static path pathinfo 0.86487698554993 fnmatch 1.0420439243317 x1.2

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

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

代码:

<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

关于php - 路径信息与 fnmatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2693428/

相关文章:

ruby-on-rails - 在 Ruby 中,如何正确读取基准测试的结果?

php - 如果文件名是 UTF-8,使 PHP pathinfo() 返回正确的文件名

Javascript函数自身调用

php - 如何从已返回模板的库函数中获取变量以在 PHP/Codeigniter 中查看

android - 遍历 SQLite Cursor 需要太多时间

PHP 从 url 获取图像扩展名?

php - 获取 PATH_INFO 的便携且安全的方法

php - 带连接和运算符的 SQL 查询

php - Symfony2 : Referrer object similar to Request object?

scala - 为什么Scala 中的这个函数调用没有被优化掉?