哪个更快:glob()或opendir()

时间:2010-05-04 06:38:44

标签: php file-io glob opendir

在glob()和opendir()之间哪个更快,用于读取大约1-2K个文件?

6 个答案:

答案 0 :(得分:8)

http://code2design.com/forums/glob_vs_opendir

显然opendir()应该(并且是)更快,因为它打开目录处理程序并让你迭代。因为glob()必须解析第一个参数,所以需要花费更多时间(加glob处理递归目录,因此它将扫描子目录,这将增加执行时间。

答案 1 :(得分:7)

globopendir执行不同的操作。 glob找到与模式匹配的路径名并在数组中返回这些路径名,而opendir仅返回目录句柄。要获得与glob相同的结果,您必须调用其他函数,在基准测试时必须考虑这些函数,特别是如果这包括模式匹配。

Bill Karwin最近写了一篇关于此事的文章。参见:

答案 2 :(得分:2)

不确定这是否是完美的比较,但是glob()允许您将类似shell的模式以及opendir直接用于那里的目录,使其更快。

答案 3 :(得分:1)

另一个问题可以通过一些测试来回答。我有一个方便的文件夹,里面有412个东西,但结果应该不会有太大变化,我想:

igor47@whisker ~/test $ ls /media/music | wc -l
412
igor47@whisker ~/test $ time php opendir.php 
414 files total

real    0m0.023s
user    0m0.000s
sys 0m0.020s
igor47@whisker ~/test $ time php glob.php 
411 files total

real    0m0.023s
user    0m0.010s
sys 0m0.010s

答案 4 :(得分:0)

好,

长话短说:

  • 如果你想要完整的文件名+路径,排序,那么glob几乎是无与伦比的。
  • 如果您想要完整的文件名+路径未排序,请使用带有GLOB_NOSORT的glob。
  • 如果您只想要名称,而不进行排序,请使用opendir + loop。

那就是它。

还有一些想法:

您可以进行测试,使用不同的方法组合完全相同的结果,但发现它们的时间成本大致相同。仅仅为了获取信息,您将没有真正的赢家。但是,请考虑以下因素:

  1. 处理一个巨大的文件列表, glob会更快地排序 - 它使用文件系统的排序方法,它总是优越的。 (它知道什么是排序,而PHP没有,PHP对任意字符串的散列数组进行排序,比较它们是不公平的。)

  2. 您可能希望通过某些扩展或文件名掩码过滤您的列表,其中glob非常有效。你当然有 fnmatch(),但每次调用它都不会比为这项工作而训练的系统级过滤器更快。

  3. 另一方面, glob 会返回更大量的文本(每个名称都带有完整路径),因此对于大量文件,您可能会遇到内存分配限制。对于zillion文件,glob不是你的朋友。

答案 5 :(得分:0)

OpenDir 更快...

<?php
    
    
    $path = "/var/Upload/gallery/TEST/";
    $filenm = "IMG20200706075415";
    
    function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }

    
    echo "<br> <i>T1:</i>".$t1 = microtime_float();
    
    echo "<br><br> <b><i>Glob :</i></b>";
    foreach( glob($path.$filenm.".*") as $file )
    {
        echo "<br>".$file;
    }
    
    echo "<br> <i>T2:</i> ".$t2 = microtime_float();
    
    echo "<br><br> <b><i>OpenDir :</b></i>";
    function resolve($name)
    {
        // reads informations over the path
        $info = pathinfo($name);
        if (!empty($info['extension']))
        {
            // if the file already contains an extension returns it
            return $name;
        }
        
        $filename = $info['filename'];
        $len = strlen($filename);
        // open the folder
        $dh = opendir($info['dirname']);
        if (!$dh)
        {
            return false;
        }
        
        // scan each file in the folder
        while (($file = readdir($dh)) !== false)
        {
            if (strncmp($file, $filename, $len) === 0)
            {
                if (strlen($name) > $len)
                {
                    // if name contains a directory part
                    $name = substr($name, 0, strlen($name) - $len) . $file;
                }
                else
                {
                    // if the name is at the path root
                    $name = $file;
                }
                closedir($dh);
                return $name;
            }
        }
        // file not found
        closedir($dh);
        return false;
    }
    
    $file = resolve($path.$filenm);
    echo "<br>".$file;
    
    echo "<br> <i>T3:</i> ".$t3 = microtime_float();
    
    echo "<br><br>&emsp; <b>glob time:</b> ". $gt= ($t2 - $t1) ."<br><b>opendir time:</b>". $ot = ($t3 - $t2) ;
    
    echo "<u>". (( $ot < $gt ) ? "<br><br>OpenDir is ".($gt-$ot)." more Faster" : "<br><br>Glob is ".($ot-$gt)." moreFaster ") . "</u>";
?>

输出:

T1:1620133029.7558

Glob :
/var/Upload/gallery/TEST/IMG20200706075415.jpg
T2: 1620133029.7929

OpenDir :
/var/Upload/gallery/TEST/IMG20200706075415.jpg
T3: 1620133029.793

  glob time:0.037137985229492
opendir time:5.9843063354492E-5

OpenDir is 0.037078142166138 more Faster
相关问题