2

I using PLINQ on collection of more then 10000 element... i m not getting the performance as compare with sequential query.

my system configuration is as follows: OS - Windows 7 32-bit, processor - Intel Core2Duo.

Please help me i am not getting the proper performance.

The query is:

ParallelQuery<int> j = Enumerable.Range(0, 1000000).AsParallel();
var sss =  j.Where(o => o%2 == 0);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
slash shogdhe
  • 3,943
  • 6
  • 27
  • 46

2 Answers2

4

As your tasks are very light-weight, a major overhead will be caused by thread context switching. Things can be improved by grouping the light-weight tasks into batches that will involve more substantial amounts of work. This can be achieved using partitioning support in PLinq. Please see the following question:

Parallel Operation Batching

By grouping the tasks you will make better use of multiple threads as the management overhead becomes less significant.

In your existing code example it is akin to fetching water from the well with a thimble, grouping the tasks is more like using a bucket.

Community
  • 1
  • 1
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
2

Even though the task is split up among threads the overhead of invoking the threads can all too often easily become more than simply running it sequential.

In this case you are doing some really simple math on it, so each operation is in the <10nsec and utilizing L1 and L2 cache very well. When you introduce threads they have a relatively high startup cost, partially because they are sleeping while waiting for someting to do.

Have a look at Custom Partitioners for PLINQ and TPL.

Tedd Hansen
  • 12,074
  • 14
  • 61
  • 97