php读取文件夹 phptxt 扫描txt文件效率 对比

zblog2年前zblog问题解答96
# 获取一个 txt 文件中的txt文件数量(一层)
function guiyi_txt_pro_txt_num2($path_txt)
{
    $path_txt = rtrim($path_txt, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; // 确保路径以斜杠结尾
    if (!is_dir($path_txt)) {
        return 0;
    }
    $txt_files = scandir($path_txt);// 一次性读取目录内容
    $txt_files = array_diff($txt_files, ['.', '..']);
    $count = 0;
    foreach ($txt_files as $file) {
        if (strtolower(substr($file, -4)) === '.txt') {
            $count++;
        }
    }
    return $count;
}

# 获取一个 txt 文件中的txt文件数量(一层)
function guiyi_txt_pro_txt_num($path_txt)
{
    $path_txt = rtrim($path_txt, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; // 确保路径以斜杠结尾
    // 检查路径是否存在且是目录
    if (!is_dir($path_txt)) {
        return 0;
    }
    $txt_files = glob($path_txt . '*.txt', GLOB_NOSORT); // 使用 glob 函数查找 .txt 文件(更高效且简洁)
    return count($txt_files);
}

$path_ceshi = "G:/phpstudy_pro/WWW/www.zblog.cn/zb_users/plugin/guiyi_txt_pro/txt/179955/";
// 使用 scandir
$start = hrtime(true);
for ($i = 0; $i < 100; $i++) {
    guiyi_txt_pro_txt_num2($path_ceshi);
}
$end = hrtime(true);
echo "scandir time: " . ($end - $start) . " seconds MY\n";
$start2 = hrtime(true);
for ($i = 0; $i < 100; $i++) {
    guiyi_txt_pro_txt_num($path_ceshi);
}
$end2 = hrtime(true);
echo "scandir time: " . ($end2 - $start2) . " seconds\n";

win下对比

scandir time: 99460000 seconds MY

scandir time: 3634966000 seconds


linux服务器对比差别不大


虽然 

$txt_files = glob($path_txt . '*.txt', GLOB_NOSORT);

 更简洁 但是 还是 自己循环效率更高

相关文章

在phpstorm中  $zbp->Load(); $zbp下面会刷红提示 未定义的变量 '$zbp' 但是在 require 'function/c_system_base.php';中已经有$GL

在phpstorm中 $zbp->Load(); $zbp下面会刷红提示 未定义的变量 '$zbp' 但是在 require 'function/c_system_base.php';中已经有$GL

在phpstorm中  $zbp->Load(); $zbp下面会刷红提示 未定义的变量 '$zbp' 但是在 require 'function/c_syst...

zblog文章重复 判断文章是否存在

1、方法1 $article = $zbp->GetListType('Post', 'select * from...

只需三步,玩转火爆全球的DeepSeek

只需三步,玩转火爆全球的DeepSeek

——小白也能轻松上手的百度千帆大模型平台指南近期,国内大模型领域迎来了一颗“新星”——DeepSeek系列模型!百度智能云千帆平台已正式上线满血版本的DeepSeek-R1和DeepSeek-V3模型...

重建表 复制表 好办法

#(1)复制表结构。 ##  从要复制的表结构复制表语句 ,设置表名字, 注意 XIUGAI ENGINE=MYISAM A...

zblog的ShowHint()函数,(后台页面顶部)输出函数,zblog提示框

zblog的ShowHint()函数,(后台页面顶部)输出函数,zblog提示框

zblog的ShowHint()函数,(后台页面顶部)输出函数,是用在后台的函数。功能是,操作提示。如图:运行效果如下,1秒后自动消失:效果如下:在插件管理页面,写测试语句:<?php ...

mysql优化表分析表重建索引

优化MySQL表是提高数据库性能的关键步骤。以下是一些常见的优化表的方法:分析表:使用 ANALYZE TABLE 命令可以更新表的统计信息,这有助于MySQL查询优化器生成更有效...