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

zblog8个月前zblog问题解答27
# 获取一个 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);

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

相关文章

豆包生成流程图 提示语 ai生图

比例 「4:3」生成 1 张极简风格的线框图,展示‘这硬盘也太烫了吧’的步骤。包含文字“这硬盘也太烫了吧”,UI要漂亮美观,颜色不要单一,要符合web视觉效果,符合网页UI规范,文字清晰规整,箭头要有...

获取zblog用户目录 zb_users目录 实际物理路径地址 怎么获取

$upload_dir = $zbp->usersdir . $upload->Dir;  # G:/phpstudy...

Nginx没有防火墙 Nginx设置加上 垃圾蜘蛛过滤

 if ($http_user_agent ~* "censys|bytedance|GPTBot|openai|Amazonbot|dotbot|c...

怎么使用百度ck 百度cookies应用到浏览器 百度账号登录免登陆

怎么使用百度ck 百度cookies应用到浏览器 百度账号登录免登陆

怎么使用百度ck 百度cookies应用到浏览器 百度账号登录免登陆Application(中文对应:应用程序(Microsoft Edge浏览器) 或 应用(谷歌chrome浏览器))BDUSS=h...

Call to undefined function xnxf()zblog提示“可能的错误原因提示或500网络错误”时排查错误的方法

Call to undefined function xnxf()zblog提示“可能的错误原因提示或500网络错误”时排查错误的方法

很多时候都有可能遇到《错误提示》这类的提示,产生这个错误的原因一般来说都是服务端报错,如下图准备工作:1、进入后台,在网站设置的全局设置里,打开调试模式。2、如果不能进后台,那就用ftp连进网站,编辑...

linux打包压缩文件夹到指定压缩包

tar -czf /www/bug_fix/c9.tar.gz --ignore-failed-read -C /www/wwwroot/8.8.8.162/runtime/cache c9打包&nb...