Dzwebs.Net

撰写电脑技术杂文十余年

rust查找文件或路径之walkdir和glob的性能对比

Admin | 2024-6-12 9:43:51 | 被阅次数 | 622

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

在rust语言中,要想查找文件或路径,可以使用walkdir和glob这两个库来完成。

然而,这两个库的性能却有明显的差异。首先看这两个库的代码。

①glob代码

fn myglob(){
    let mut c = 0;
    let options = MatchOptions {
        case_sensitive: false,
        require_literal_separator: false,
        require_literal_leading_dot: false,
    };
    for entry in glob_with("c:/**/*.txt", options).unwrap() {
        if let Ok(path) = entry {
            println!("{:?}", path.display());
            c +=1;
        }
    }
    println!("一共找到{}个文件", c);
}

②walkdir代码

fn mywalkdir(){
let mut c = 0;
WalkDir::new("c:/").into_iter().filter_map(|e| e.ok()).filter(|e|e.file_type().is_file()).for_each(|e|{
let filetype =[".txt"];
        if filetype.iter().any(|x|
            e.path().display().to_string().to_ascii_uppercase().ends_with(x.to_ascii_uppercase().as_str()))
            && !e.path().display().to_string().contains("$"){
            println!("文件路径:{}", e.path().display());
            c +=1;
        }
    });
    println!("一共找到{}个文件", c);
}

上述的两种库和对应的代码,扫描C盘下所有的txt文件,都扫描到4173个文件,但是glob用了404秒,但是walkdir只使用了23秒,显然,后者性能更好速度更快。


该杂文来自: 最新技术

上一篇:rust设置编译的exe的ico图标的方法

下一篇:暂时没有文章了

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计