代码之家  ›  专栏  ›  技术社区  ›  zaf

pathinfo与fnmatch

  •  6
  • zaf  · 技术社区  · 15 年前

    关于fnmatch在pathinfo上的速度有一个小争论: how to check if file is php?

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

    使用动态和静态路径表明pathinfo更快。

    我的基准逻辑和结论有效吗?

    编辑 :从命令使用mac php

    PHP5.3.0(cli)(构建时间:2009年7月20日 13:56:33)版权所有(c)1997-2009 php组zend引擎v2.3.0, 版权所有(c)1998-2009 zend 技术

    动态路径路径信息3.2973630428314 Fnmatch 3.4520659446716 x1.05

    静态路径路径信息0.86487698554993 匹配1.0420439243317 x1.2

    来自CMD的Mac Xampp php

    PHP5.3.1(cli)(构建时间:2010年2月27日 12:41:51)版权所有(c)1997-2009 php组zend引擎v2.3.0, 版权所有(c)1998-2009 zend 技术

    动态路径路径信息3.63922715187 Fnmatch 4.99041700363 x1.37

    静态路径pathinfo 1.03110480309 Fnmatch 2.38929820061 x2.32

    我在我的机器上包含了一个以秒为单位的100000次迭代的结果示例:

    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>
    
    5 回复  |  直到 15 年前
        1
  •  1
  •   OIS    15 年前

    我的结果和你的相反:

    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
    
        2
  •  1
  •   Ben    15 年前

    使用相同的基准代码

    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
    
        3
  •  1
  •   Gordon    15 年前

    用你的

    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
    

    给予

    dynamic path
    pathinfo 3.4931519031525
    fnmatch 2.8633069992065
    x0.82
    
    static path
    pathinfo 0.83261299133301
    fnmatch 0.28636598587036
    x0.34
    

    这四

    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);
    }
    

    像这样跑的时候

    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__);
    

    在我的Zend Studio Give的机器上

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

    我觉得4号是最快的有点烦人,但它就是这么说的。每次呼叫0.00000N秒也没什么麻烦。

        4
  •  1
  •   webbiedave    15 年前

    我会把我的结果放在这里:

    dynamic path
    pathinfo 4.9078891277313
    fnmatch 4.3466200828552
    x0.89
    
    static path
    pathinfo 1.4787950515747
    fnmatch 0.98351812362671
    x0.67
    

    但请记住我对您所链接的问题的原始评论:

    关于速度,fnmatch将出局 执行pathinfo 2:1 当用于 这个目的。

    特定于特定扩展上的分支。

        5
  •  0
  •   Community CDub    8 年前

    我对所有答案都投了赞成票,但会回答我自己的问题。

    我的基准逻辑和结论是有效的,所有的答案基准都是有效的。

    我已经找到了原因,它提出了另一个问题,但更确切地说,使这篇文章在切线,使它更长,我将打开另一个问题。我做完后会把新的链接放在这里。

    谢谢你为我做基准测试!

    编辑:下面是第二部分的问题: (Pathinfo vs fnmatch part 2) Speed benchmark reversed on Windows and Mac