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

zblog1年前zblog问题解答69
# 获取一个 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...

php7防止跨站  防跨站攻击(open_basedir),防止黑客通过其他网站目录进行入侵攻击 防跨站攻击

php7防止跨站 防跨站攻击(open_basedir),防止黑客通过其他网站目录进行入侵攻击 防跨站攻击

堡塔PHP安全扩展,用于增强防跨站机制,建议Nginx/Apache用户安装防跨站攻击(open_basedir),防止黑客通过其他网站目录进行入侵攻击php7.2存在问题 7.4测试不存子啊...

brotli 命令行工具 在centos 和 Ubuntu 分别怎么安装

在CentOS和Ubuntu上安装brotli命令行工具,可以按照以下步骤进行:在CentOS上安装brotli更新系统(可选但推荐):首先,确保你的CentOS系统已经更新到最新状态。bash复制代...

echo plugin_dir_url(__FILE__); zblog 通过文件获取应用URL地址 返回插件目录的URL地址

<?php /** @var ZBLOGPHP $zbp */ require_once '../../../../zb...

php exlpode 字符串转数组函数(普通字符串 分割 + 正则表达式 分割)

============普通字符串 分割explode() 函数是 PHP 中的一个非常实用的字符串处理函数,它的作用是将一个字符串按照指定的分隔符分割成数组。这个函数经常用于处理 CSV 文件、UR...

zblog批量删除文章 删除文章代码

############################## $where = array(); $where[] = array('='...