Click here to Skip to main content
Click here to Skip to main content

When to use PLINQ vs LINQ

, 27 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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;
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

License

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

About the Author

Tony Dubey

United States United States
No Biography provided

Comments and Discussions

 
QuestionHow do you define expensive operation and not expensive operation? PinmemberMember 1011124712-Jun-14 18:56 
GeneralMixed Results PinmemberMember 77888833-May-12 3:26 
GeneralRe: Mixed Results PinmemberAndré Ziegler3-May-12 4:08 
GeneralRe: Mixed Results PinmemberKim Nordmo3-May-12 8:35 
GeneralRe: Mixed Results PinmemberMember 209996822-May-12 10:13 
QuestionI think your CPU is the issue PinmemberAndré Ziegler1-May-12 10:40 
GeneralMy vote of 5 Pinmembermember6027-Apr-12 18:18 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 27 Apr 2012
Article Copyright 2012 by Tony Dubey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid