2012-02-24 4 views
3

私はEmpIdsのリストを持っています。このリストの各EmpIDについて、いくつかの操作を実行する必要があります。AsParallelの使用

void ProcessEmp(EmpId empId) 
{ 
    // process the emp 
} 

ではなく時に各従業員ID 1をループ、私はAsParallelを使用してこの操作を行うことができますか?基本的に私は各emp Idを並列に処理したい。

+3

のですか? CPUの処理量はどのように処理されますか?私は本当にパラレルがもっと速いかどうかにかかっています。あなたはどんなタイミングをしましたか?これはあなたのボトルネックですか? – BrokenGlass

+2

どのリストにありますか?あなたの例では関数宣言があります。リストがIEnumerableをサポートしている場合は、Parallel.Foreachを使用できます。 –

+1

@BrokenGlass EmpIDリストには約80のアイテムがあります。 Noramalプロセスは、これらのemp IDを処理するのに約20分かかります。私はそれがより速くできるかどうか疑問に思っていた。 – RRForUI

答えて

5

むしろよりAsParallel、あなたはおそらくParallel.ForEachをしたい:

Parallel.ForEach(ids, ProcessEmp); 

それとも、メソッドグループの変換が気に入らない場合:

Parallel.ForEach(ids, id => 
{ 
    Console.WriteLine("Processing {0}", id); 
    ProcessEmp(id); 
}); 
+0

ありがとうございました。私はparallel.ForEachを使って試しました。しかし、それはパフォーマンスの向上をもたらしません。プログラムはまだ実行するのに20分かかります – RRForUI

+0

@rohit:何がやっているのかわからないので、何が間違っているのかは分かりません。たとえば、ボトルネックは外部リソースです。 –

0

は、あなたがリストを持っているんどのように多くの項目のサンプル例

string[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"); 
string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified"; 
System.IO.Directory.CreateDirectory(newDir); 

// Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body) 
Parallel.ForEach(files, currentFile => 
{ 
    // The more computational work you do here, the greater 
    // the speedup compared to a sequential foreach loop. 
    string filename = System.IO.Path.GetFileName(currentFile); 
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile); 

    bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); 
    bitmap.Save(System.IO.Path.Combine(newDir, filename)); 

    // Peek behind the scenes to see how work is parallelized. 
    // But be aware: Thread contention for the Console slows down parallel loops!!! 
    Console.WriteLine("Processing {0} on thread {1}", filename, 
    Thread.CurrentThread.ManagedThreadId); 

    } //close lambda expression 
); //close method invocation 
関連する問題