Click here to Skip to main content
15,558,978 members
Articles / Programming Languages / C# 4.0
Tip/Trick
Posted 27 Apr 2012

Tagged as

Stats

29.5K views
15 bookmarked

When to use PLINQ vs LINQ

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
27 Apr 2012CPOL
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.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

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

        public bool ExpensiveComputation()
        {
            Thread.Sleep(10);
            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();
            /////////////////////////////////////////////////

            Console.ReadKey();

        }

        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

Image 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow do you define expensive operation and not expensive operation? Pin
Member 1011124712-Jun-14 19:56
Member 1011124712-Jun-14 19:56 
GeneralMixed Results Pin
Kim Nordmo3-May-12 4:26
Kim Nordmo3-May-12 4:26 
GeneralRe: Mixed Results Pin
André Ziegler3-May-12 5:08
André Ziegler3-May-12 5:08 
GeneralRe: Mixed Results Pin
Kim Nordmo3-May-12 9:35
Kim Nordmo3-May-12 9:35 
GeneralRe: Mixed Results Pin
Member 209996822-May-12 11:13
Member 209996822-May-12 11:13 
QuestionI think your CPU is the issue Pin
André Ziegler1-May-12 11:40
André Ziegler1-May-12 11:40 
GeneralMy vote of 5 Pin
member6027-Apr-12 19:18
member6027-Apr-12 19:18 
Helpful !

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.