pathinfo vs fnmatch

ぐ巨炮叔叔 提交于 2019-12-01 16:31:25

My results are opposite of yours:

php -f 2693428.php
dynamic path
pathinfo 4.5834331512451
fnmatch 3.2174317836761
x0.7

static path
pathinfo 2.1787130832672
fnmatch 0.95714497566223
x0.44

Version

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

Using the same benchmarking code

dynamic path
pathinfo 2.6542711257935
fnmatch 1.9943950176239
x0.75

static path
pathinfo 1.1711349487305
fnmatch 0.54186105728149
x0.46

PHP Version 5.3.1
Build Date  Nov 20 2009 17:20:57
Compiler    MSVC6 (Visual C++ 6.0)
Architecture    x86
Thread Safety   enabled
Zend Memory Manager enabled
Zend Multibyte Support  disabled
Gordon

Running yours with

PHP 5.3.2 (cgi-fcgi) (built: Mar  3 2010 20:47:00)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Zend Debugger v5.3, Copyright (c) 1999-2010, by Zend Technologies

gives

dynamic path
pathinfo 3.4931519031525
fnmatch 2.8633069992065
x0.82

static path
pathinfo 0.83261299133301
fnmatch 0.28636598587036
x0.34

These four

function hasExtension1($ext, $filename)
{
    return !strcasecmp(pathinfo($filename, PATHINFO_EXTENSION), $ext);
}

function hasExtension2($ext, $filename)
{
    return fnmatch("*.$ext", $filename, FNM_CASEFOLD);
}

function hasExtension3($ext, $filename)
{
    return strripos($filename, $ext) === strlen($filename) - strlen($ext);
}

function hasExtension4($ext, $filename)
{
    return !strcasecmp(substr($filename, -strlen($ext)), $ext);
}

when run like this

for($i=0;$i<10000;$i++) hasExtension1('php', __FILE__);
for($i=0;$i<10000;$i++) hasExtension2('php', __FILE__);
for($i=0;$i<10000;$i++) hasExtension3('php', __FILE__);
for($i=0;$i<10000;$i++) hasExtension4('php', __FILE__);

and profiled on my machine from Zend Studio give

Average Mean Time = 0.000007
Average Mean Time = 0.000006
Average Mean Time = 0.000005
Average Mean Time = 0.000003

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:

dynamic path
pathinfo 4.9078891277313
fnmatch 4.3466200828552
x0.89

static path
pathinfo 1.4787950515747
fnmatch 0.98351812362671
x0.67

But keep in mind my original comment on the question you linked to:

Concerning speed, fnmatch will out perform pathinfo 2:1 when used for this purpose.

Specific to branching on a particular extension.

zaf

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!