13,256,716 members (51,848 online)
Tip/Trick
alternative version

#### Stats

19.7K views
16 bookmarked
Posted 27 Apr 2012

# When to use PLINQ vs LINQ

, 27 Apr 2012
 Rate this:
This code demonstrate pros and cons of PLINQ as compared to LINQ

## Introduction

Be careful when you are using PLINQ. What I found is if the computation is very simple and non-expensive, it is better to use LINQ than PLINQ.

## Using the code

Following code is tested in multicore machine (8 CPU). First I timed a CPU intensive code with LINQ and then with PLINQ. Then I timed NON CPU intensive code with LINQ and PLINQ.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace PLinqSample
{
public class Employee
{
public string Name { get; set; }
public double Salary { get; set; }

public bool ExpensiveComputation()
{
return (Salary > 2000 && Salary < 3000);
}

public bool NonExpensiveComputation()
{
return (Salary > 2000 && Salary < 3000);
}
}

class Program
{
static void Main(string[] args)
{
List<Employee> employeeList = GetData();

Stopwatch stopWatch = new Stopwatch();

///////////////////////////////// Testing Old LINQ With Expensive Computation///////////////
stopWatch.Start();

var linqResult = employeeList.Where<Employee>(e=> e.ExpensiveComputation());

int empCount = linqResult.Count();

stopWatch.Stop();

Console.WriteLine(string.Format("Time taken by old LINQ in Expensive Computation is {0} to get {1} Employees", stopWatch.Elapsed.TotalMilliseconds, empCount));

stopWatch.Reset();
//////////////////////////////////////////////////////////

///////////////////////////////// Testing NEW PLINQ With Expensive Computation/////////////
stopWatch.Start();

linqResult = employeeList.AsParallel<Employee>().Where<Employee>(e=> e.ExpensiveComputation());

empCount = linqResult.Count();

stopWatch.Stop();

Console.WriteLine(string.Format("Time taken by new PLINQ in Expensive Computation is {0} to get {1} Employees", stopWatch.Elapsed.TotalMilliseconds, empCount));

stopWatch.Reset();
/////////////////////////////////////////////////

Console.WriteLine();

///////////////////////////////// Testing Old LINQ With NON Expensive Computation///////////////
stopWatch.Start();

linqResult = employeeList.Where<Employee>(e => e.NonExpensiveComputation());

empCount = linqResult.Count();

stopWatch.Stop();

Console.WriteLine(string.Format("Time taken by old LINQ in Non Expensive Computation is {0} to get {1} Employees", stopWatch.Elapsed.TotalMilliseconds, empCount));

stopWatch.Reset();
//////////////////////////////////////////////////////////

///////////////////////////////// Testing NEW PLINQ With NON Expensive Computation/////////////
stopWatch.Start();

linqResult = employeeList.AsParallel<Employee>().Where<Employee>(e => e.NonExpensiveComputation());

empCount = linqResult.Count();

stopWatch.Stop();

Console.WriteLine(string.Format("Time taken by new PLINQ in Non Expensive Computation is {0} to get {1} Employees", stopWatch.Elapsed.TotalMilliseconds, empCount));

stopWatch.Reset();
/////////////////////////////////////////////////

}

static List<Employee> GetData()
{
List<Employee> employeeList = new List<Employee>();
Random random = new Random(1000);

for (int i = 0; i < 1000; i++)
{
employeeList.Add(new Employee() { Name = "Employee" + i, Salary = GetRandomNumber(random, 1000, 5000)});
}

return employeeList;
}

static double GetRandomNumber(Random random, double minimum, double maximum)
{
return random.NextDouble() * (maximum - minimum) + minimum;
}
}
}

Here is the result of above code

## Share

 United States
No Biography provided

## You may also be interested in...

 Pro Pro

 First Prev Next
 How do you define expensive operation and not expensive operation? Member 1011124712-Jun-14 19:56 Member 10111247 12-Jun-14 19:56
 Mixed Results Member 77888833-May-12 4:26 Member 7788883 3-May-12 4:26
 Re: Mixed Results André Ziegler3-May-12 5:08 André Ziegler 3-May-12 5:08
 Re: Mixed Results Kim Nordmo3-May-12 9:35 Kim Nordmo 3-May-12 9:35
 I did read your post as well before I posted. I posted the result since I was perplexed by the performance gain when NOT using the debugging function in visual studio, and regarding the intels hyper-threading this isn't something I can comment on with my limited knowledge of intel and its hyper-threading.
 Re: Mixed Results Member 209996822-May-12 11:13 Member 2099968 22-May-12 11:13
 I think your CPU is the issue André Ziegler1-May-12 11:40 André Ziegler 1-May-12 11:40
 My vote of 5 member6027-Apr-12 19:18 member60 27-Apr-12 19:18
 Last Visit: 31-Dec-99 19:00     Last Update: 23-Nov-17 0:00 Refresh 1