具有批处理逻辑的循环的C#和并行

时间:2017-04-11 12:33:53

标签: c# asp.net c#-4.0 parallel-processing batch-processing

我编写了一个for循环,用于处理以20个为一批的列表添加项目:

  for (int i = 0; i < filteredList.Count; i = i + 20)
  {
    newlist.Add(GetResponse(filteredList.Skip(i).Take(20)));
  }

这适用于较小的列表。所以现在我正在考虑加快速度,当我使用内部有10-15000个项目的大型列表来使用并行循环时?

我试过这样的事情:

Parallel.For(0,filteredList.Count, i=>{
i+=20;
newlist.Add(GetResponse(filteredList.Skip(i).Take(20)));
});

但是这并没有给我带来想要的结果......有没有什么方法可以执行除常规for循环之外的批量插入,以更快的方式执行时间比plain for循环更好?

1 个答案:

答案 0 :(得分:4)

set term postscript eps enhanced color "Helvetica" 10 set output "dosband.eps" set title "Bandstructure and Density of States" # set multiplot layout 1,2 \ margins 0.075,0.98,0.1,0.98 \ spacing 0.02,0.08 #margins: left,right,bottom,top; spacing: vertical, horizontal set title "Bandstructure" plot 'plotband.dat' using 1:2 with lines lt 1 lw 0.5 linecolor rgb "black" notitle set xlabel "Density [states/eV]" #dont ask me why I have to swap the xlabels around set ylabel "Energy [eV]" # set title "Density of States" plot 'plotdos.dat' using 1:2 with lines lt 1 linecolor rgb "black" notitle set xlabel "K-Points" unset multiplot 线程安全,这就是

的原因
List<T>

是一种错误的技术。请尝试使用 Parallel Linq (Plinq):

  Parallel.For(... {
    ...
    newlist.Add(GetResponse(filteredList.Skip(i).Take(20)));
    ...
  });