Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何使用AsParallel()/Parellel.ForEach()的编码指南_C#_.net_Multithreading_Parallel Processing - Fatal编程技术网

C#如何使用AsParallel()/Parellel.ForEach()的编码指南

C#如何使用AsParallel()/Parellel.ForEach()的编码指南,c#,.net,multithreading,parallel-processing,C#,.net,Multithreading,Parallel Processing,我想计算10000条文本的Jaccard相似性。 Jaccard相似度很容易计算:相交长度除以并集长度 string sTtxt1 = "some text one"; string sTtxt2 = "some text two"; string sTtxt3 = "some text three"; HashSet<string[]> hashText= new HashSet<string[]>(); hashText.Add(sTtxt1); hashText.A

我想计算10000条文本的Jaccard相似性。 Jaccard相似度很容易计算:相交长度除以并集长度

string sTtxt1 = "some text one";
string sTtxt2 = "some text two";
string sTtxt3 = "some text three";
HashSet<string[]> hashText= new HashSet<string[]>();
hashText.Add(sTtxt1);
hashText.Add(sTtxt2);
hashText.Add(sTtxt3);
double[,] dSimilarityValue;

for (int i = 0; i < hashText.Count; i++)
{
   dSimilarityValue[i, i] = 100.00;
   for (int j = i + 1; j < dSimilarityValue.Count; j++)
   {
      dSimilarityValue[i, j] = (double) hashText.ElementAt(i).Intersect(hashText.ElementAt(j)).Count() / (double) hashText.ElementAt(i).Union(hashText.ElementAt(j)).Count();
   }
}
string sTtxt1=“some text one”;
string sTtxt2=“一些文本二”;
string sTtxt3=“一些文本三”;
HashSet hashText=新的HashSet();
hashText.Add(sTtxt1);
hashText.Add(sTtxt2);
hashText.Add(sTtxt3);
双[,]d相似值;
for(int i=0;i
对于.NET4,我应该使用什么规则来并行化


谢谢大家!

只需使内环平行即可

我认为最好用new声明数组的大小。

我不知道你所说的“规则”是什么意思。

bum,说到“规则”,我想说的是“方法”(对不起,我的英语很差,我是法国人;))它与Parallel很好。我把处理时间缩短了10分钟。谢谢你的帮助。我测试了
(double)hashText.AsParallel().ElementAt(I).Intersect(hashText.ElementAt(j)).Count()/(double)hashText.AsParallel().ElementAt(I).Union(hashText.ElementAt(j)).Count()但性能下降ElementAt()是单数的,因此无法启动多个线程。该内部循环是并行的逻辑点。因此,使用另一个
HashSet
的数据结构来并行化Intersect()&Union()。但是哪一个呢?HashSet很好。正如我所说,内部循环是并行的逻辑点。
Parallel.For(0, N, i =>
{
   // Do Work.
}); 


Parallel.For(j, dSimilarityValue.Count, i =>
{
   dSimilarityValue[i, j] = 
    (double)hashText.ElementAt(i).Intersect(hashText.ElementAt(j)).Count() / 
    (double)hashText.ElementAt(i).Union(hashText.ElementAt(j)).Count();
});