- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已将以下算法写入 C# 代码以递归列出文件夹中的文件。
在包含 50,000 个文件和 12,000 个子目录的文件夹上执行速度非常慢。由于磁盘读取操作基本上是耗时的任务。即使 LINQ.Parallel() 也无济于事。
实现:
class FileTuple
{
public string FileName { set; get; }
public string ContainingFolder { set; get; }
public bool HasDuplicate { set; get; }
public override bool Equals(object obj)
{
if (this.FileName == (obj as FileTuple).FileName)
return true;
return false;
}
}
以下方法查找重复文件并作为列表返回。
private List<FileTuple> FindDuplicates()
{
List<FileTuple> fileTuples = new List<FileTuple>();
//Read all files from the given path
List<string> enumeratedFiles = Directory.EnumerateFiles(txtFolderPath.Text, "*.*", SearchOption.AllDirectories).Where(str => str.Contains(".exe") || str.Contains(".zip")).AsParallel().ToList();
foreach (string filePath in enumeratedFiles)
{
var name = Path.GetFileName(filePath);
var folder = Path.GetDirectoryName(filePath);
var currentFile = new FileTuple { FileName = name, ContainingFolder = folder, HasDuplicate = false, };
int foundIndex = fileTuples.IndexOf(currentFile);
//mark both files as duplicate, if found in list
//assuming only two duplicate file
if (foundIndex != -1)
{
currentFile.HasDuplicate = true;
fileTuples[foundIndex].HasDuplicate = true;
}
//keep of track of the file navigated
fileTuples.Add(currentFile);
}
List<FileTuple> duplicateFiles = fileTuples.Where(fileTuple => fileTuple.HasDuplicate).Select(fileTuple => fileTuple).OrderBy(fileTuple => fileTuple.FileName).AsParallel().ToList();
return duplicateFiles;
}
能否请您提出一种提高性能的方法。
感谢您的帮助。
最佳答案
Can you please suggest a way to improve the performance.
一个明显的改进是使用 Dictionary<FileTuple, FileTuple>
以及 List<FileTuple>
.这样你就不会有 O(N) IndexOf
对每张支票进行操作。请注意,您还需要覆盖 GetHashCode()
- 你应该已经有关于此的警告。
不过我怀疑它会产生很大的不同 - 我希望这主要是 IO 绑定(bind)的。
此外,我怀疑最后的过滤和排序会成为一个重大瓶颈,所以使用 AsParallel
在最后一步不太可能做太多。当然,您应该衡量所有这些。
最后,整个方法可以变得更简单,甚至不需要 HasDuplicate
标志或任何覆盖 Equals
/GetHashCode
:
private List<FileTuple> FindDuplicates()
{
return Directory.EnumerateFiles(txtFolderPath.Text, "*.*",
SearchOption.AllDirectories)
.Where(str => str.Contains(".exe") ||
str.Contains(".zip")
.Select(str => new FileTuple {
FileName = Path.GetFileName(str),
ContainingFolder = Path.GetDirectoryName(str))
})
.GroupBy(tuple => tuple.FileName)
.Where(g => g.Count() > 1) // Only keep duplicates
.OrderBy(g => g.Key) // Order by filename
.SelectMany(g => g) // Flatten groups
.ToList();
}
关于c# - 在 C# : Leveraging LINQ. AsParallel 中列出文件夹内的重复文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763797/
我想知道 C# 中 AsParallel 和 AsParallel.Select() 之间的区别是什么,因为两者都返回 AsParallelQuery 的实例。考虑以下代码片段: var list =
如何限制AsParallel()预先读取并放入其内部缓冲区的项目数量? 这是一个例子: int returnedCounter; IEnumerable Enum() { while (tru
我有一个 EmpId 列表。我需要为此列表中的每个 EmpId 执行一些操作。 void ProcessEmp(EmpId empId) { // process the emp } 我可以使用
我毫不怀疑,对于客户端应用程序,AsParallel() 会带来一些开箱即用的性能提升。但是如果我在网络环境中使用它呢?假设我有一个小部件框架,它循环遍历所有小部件以获取它们的数据并呈现输出。这会很好
Stephen Toub 的书第 33 页 http://www.microsoft.com/download/en/details.aspx?id=19222 有代码 var pings = fro
我有以下 PLINQ 查询: // Let's get a few customers List customers = CustomerRepository.GetSomeCustomers();
下面的测试程序好像不会做深蹲。这是因为我正在测试一个小列表吗? static void Main(string[] args) { List list = 0.UpTo(4); Tes
所以主题就是问题。 我得到 AsParallel 方法返回包装器 ParallelQuery使用相同的 LINQ 关键字,但来自 System.Linq.ParallelEnumerable而不是 S
我看过这段代码,它使用 AsParallel() 和 Any() 来检查条件: bool IsAnyDeviceConnected() { return m_devices.Any(d => d
谁能给我解释一件事。据我了解 AsParallel() 在自己的任务中执行。那么,如果查询返回大量数据,当'foreach'开始执行Console.WriteLine时,变量'd'可以为空吗? var
在使用 Parallel.ForEach 时,我们可以选择定义并行选项并设置最大并行度,例如: Parallel.ForEach(values, new ParallelOptions {MaxDeg
我想使用 PLINQ 方法 AsParallel() 重写这个 Parallel.For 循环。据我所知, AsParallel() 需要将整个序列传递给它,例如创建数组 A,然后调用 A.AsPar
我有一个 List收藏,每个Boss都有2到10个助理职员。我正在对包括老板在内的所有员工进行分组。现在我有 List ,由此我正在使用 Parallel LINQ 搜索 "Raj",我可以在哪里放置
这段代码有什么区别: int[] tab = new int[] { 1, 2, 3, 4, 5 }; List result1 = (from t in tab
当我使用 AsParallel() 时,下一个操作在多线程中运行,那么,我在此查询中使用的方法应该是线程安全的? 在下一个示例中,Convert(string value) 方法不是线程安全的,但在作
假设执行一个方法 CountString,给定一个字符串数组和一个 int,返回长度大于该 int 的字符串的数量。如果我必须尽可能利用多核硬件,这样做是否足够: public int CountSt
我正在构建一个必须处理一堆文档的控制台应用程序。 为了简单起见,过程是: 对于 X 和 Y 之间的每一年,查询数据库以获取流程的文档引用列表 对于每个引用,处理一个本地文件 我认为 process 方
我用 List.AsParallel().WithDegreeOfParallelism(3).ForAll(x => Worker(x)); 在列表的每个元素上应用 Worker。 我如何命名运行
我对使用 LINQ AsParallel() 的并发性有一些疑问。 假设我有以下代码: int counter = 0; someList.AsParallel().ForEach(item => {
感谢观看。我有以下返回语句: //Return the result set return new FilterDto.FilterResult
我是一名优秀的程序员,十分优秀!