I’m trying to implement PLINQ example but facing following problem
My sequential queries are executed faster than parallel queries.
here is the code example:
Stopwatch sw = new Stopwatch();
int[] vals = Enumerable.Range(0, Int16.MaxValue).ToArray();
sw.Start();
int[] x1 = vals.Where(x => x % 2 == 0).ToArray();
sw.Stop();
Console.WriteLine("Sequential Execution {0} milliseconds", sw.ElapsedMilliseconds);
sw.Restart();
int[] x2 = vals.AsParallel().Where(x => x % 2 == 0).ToArray();
sw.Stop();
Console.WriteLine("Parallel Execution {0} milliseconds", sw.ElapsedMilliseconds);
My machine is Pentium(R) Dual – Core
I’ve also tried on Quad – Core AMD Opteron(tm).
The same result Parallel queries run slower than sequential.
Can you please tell me what is my problem?
Thanks.
this one seems to work better:
do not start another thread for 200 values. it takes more to start/wake up the other threads than to finish the entire loop on a single thread. + more threads mean thread syncronization mechanism.
LE: Ok, I tried for Int16.MaxValue and it works better there. Not I realized that the maxvalue is around 30k, so the comment may not apply to your case. probably the issue is that AsParralel was missplaced.