-1

i like to know What is the difference between LINQ & PLINQ in terms of execution ?

when we should use PLINQ not LINQ just discuss some situation

here giving two code. one done by LINQ and other done by PLINQ.

just tell me what is the difference and what the two code is doing?

LINQ

var q = from file in fileList  
         where file.Length > 10000 
        select file; 

PLINQ

var q = from file in fileList.AsParallel()
         where file.Length > 10000                      
        select file;

please see the above code and tell me how both the code execute and what is the difference in terms of execution? thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • _"what is the difference?"_ In this case probably nothing. It depends on what kind of thing `fileList` is. – H H Dec 03 '14 at 14:41
  • While they looks *interchangeable*, they are used to solve specific problems. If you don't know cases, then your question automatically become [What is PLINQ](http://stackoverflow.com/q/1663022/1997232) (or [worse](http://stackoverflow.com/q/471502/1997232)). P.S.: I can easily recall your name now, because you are asking a lot of *strange* questions. It's ok for a kid to ask *why?*, otherwise you have to provide *clear reasoning* of question. Without it you may create hundreds of *useless questions*. – Sinatr Dec 03 '14 at 15:20
  • I think your examples are not great, because neither of them actually *executes* anything. You would need to add `ToList()` or `foreach` or something like that for them to actually do something. – svick Dec 03 '14 at 17:44

2 Answers2

3

PLINQ (and paralellism in general) only offers a potential performance benefit when the load can be spread across CPU cores. Parallelism does not improve disk I/O, network I/O or remote resources. It can improve memory access, but memory is generally bandwidth limited as well.

Run your code and observe the CPU usage. If one core of the CPU jumps to 100%, then CPU is a bottleneck and you might benefit from parallelism; otherwise CPU is not your bottleneck, and running in parallel probably won't improve your performance.

Note that, in the end, the only way to know for sure is to try it both ways and measure the difference. Anything else is just speculation.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I was trying to know how many thread or process will run parallel to extract files from filelist if i use plinq? Please see my plinq code and guide me.thanks – Thomas Dec 03 '14 at 16:36
0

PLinq is the parallel version of Linq which means that some queries can be executed on multiple threads and then PLinq gives a performance increase. The trade-offs are the same as if you're choosing between using parallel programming or sequential Choosing between both.

See this Might be helpful link

Community
  • 1
  • 1
yadrimz
  • 26
  • 2
  • 3
    `PLinq` is not always faster. http://msdn.microsoft.com/en-us/library/dd997399.aspx – Tim Schmelter Dec 03 '14 at 14:29
  • @yadrimz: u said "which means that some queries can be executed on multiple threads" can we see how many thread is working to execute my query? can we set no of thread should handle my query ? please guide. – Thomas Dec 04 '14 at 07:05
  • Try with this: Console.WriteLine("{0} threads " ,Process.GetCurrentProcess().Threads.Count); See also this [link](http://stackoverflow.com/questions/1812657/why-does-plinq-use-only-two-threads) Note: Usually plinq will choose a degree of parallelism equal to your core count, however see this [might be useful link](http://stackoverflow.com/questions/6843820/plinq-how-to-run-a-parallelquery-on-more-than-4-threads) – yadrimz Dec 04 '14 at 11:21