枚举文件:哪个更快 [英] Enum files : Which is Faster

查看:101
本文介绍了枚举文件:哪个更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任意长度的所需文件扩展名列表.

枚举目录以搜索具有这些扩展名的文件时(如果需要的话,使用Directory.EnumerateFiles),您认为哪一种会更快?

A)获取完整的文件列表,然后忽略与可接受的扩展名不匹配的文件?



B)获取多个文件列表,每个列表仅包含匹配的扩展名?

我倾向于B,这仅仅是因为A可能会创建大量要过滤的文件列表,而B会被预先过滤.

但是,一遍又一遍地回到相同的目录中查找不同的文件类型是否会比一味地将它们全部添加在一起增加更多的开销呢?

-

枚举文件时使用的查询为:

I have a arbitrary length list of sought-for file extensions.

When enumerating directories in search of files with those extensions (using Directory.EnumerateFiles, if it matters), which do you think would be faster?

A) Get a complete list of files, then ignore any that don''t match the acceptable extensions?

or

B) Get multiple lists of files, each containing only those extensions that match?

I''m leaning toward B, if only because A could potentially create a huge list of files to filter through, whereas B comes pre-filtered.

But does going back into the same directories over and over to look for different file types add more overhead than just getting them all in one fell swoop?

--

The query used in enumerating the files is:

var TheFiles = from file in
               Directory.EnumerateFiles(SearchPath, "*.*", SO)
               where file.Length > 0
               select file;



更改"where"子句会有所帮助吗?像

文件所在位置.长度> 0 AND(file.EndsWith(Ext1)或(file.EndsWith(Ext2))

etc



Would changing the "where" clause help? Something like

where file.Length > 0 AND (file.EndsWith(Ext1) OR (file.EndsWith(Ext2))

etc

推荐答案

我有个主意.既然您是一名程序员,为什么不尝试两种方式来回答自己的问题?

编辑--------------

我将使用DirectoryInfo.GetFiles("*.xyz", true);并完成它.顺便说一句,LINQ总是比较慢.
I have an idea. Since you''re a programmer, why don''t you try both ways and answer your own question?

EDIT --------------

I would use DirectoryInfo.GetFiles("*.xyz", true); and be done with it. BTW, LINQ is always slower.


这篇关于枚举文件:哪个更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆