【发布时间】:2015-03-14 23:01:19
【问题描述】:
到目前为止,我使用以下代码通过哈希码将文件条目列表与 itef 进行比较
for (int i = 0; i < fileLists.SourceFileListBefore.Count; i++) // Compare SourceFileList-Files to themselves
{
for (int n = i + 1; n < fileLists.SourceFileListBefore.Count; n++) // Don´t need to do the same comparison twice!
{
if (fileLists.SourceFileListBefore[i].targetNode.IsFile && fileLists.SourceFileListBefore[n].targetNode.IsFile)
if (fileLists.SourceFileListBefore[i].hash == fileLists.SourceFileListBefore[n].hash)
{
// do Something
}
}
}
SourceFileListBefore 是一个列表
我想更改此代码以便能够在多个内核上并行执行。我想过用 PLINQ 来做这件事,但我对 LINQ 完全陌生。
我试过了
var duplicate = from entry in fileLists.SourceFileListBefore.AsParallel()
where fileLists.SourceFileListBefore.Any(x => (x.hash == entry.hash) && (x.targetNode.IsFile) && (entry.targetNode.IsFile))
select entry;
但它不会像这样工作,因为我必须为每对两个哈希码匹配条目执行代码。因此,我至少必须从 LINQ 获取带有 x+entry 的结果集合,而不仅仅是一个条目。 PLINQ 可以做到吗?
【问题讨论】:
-
I used up until now to compare a list of file-entrys to itsef by hash-codes你的真正意图是什么?在我看来XY-problem -
为什么要并行执行此操作?你在计算散列吗?如果没有,那么您的清单必须很大才能有所作为。
-
它可能是一个包含 100k 个文件的列表,因此可能需要一段时间。我必须立即为我的程序计算列表。