繁体   English   中英

Plinq和linq性能对比c#

[英]Plinq and linq performance comparison c#

有人可以为我解释为什么PLinq在第一个例子中比普通的linq好,但在第二个例子中最差? 我看到唯一的区别是 Thread.Sleep() 在 ExpensiveComputation() function

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

namespace PlinqTest
{
    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();

            // Example 1

            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();
            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();

            // Example 2

            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();
            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 < 500; 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;
        }
    }
}

结果如下:

老 LINQ 在昂贵的计算中花费的时间是5458.0269以获得 135 个员工

新 PLINQ 在昂贵的计算中花费的时间是741.835来获得 135 名员工

旧 LINQ 在非昂贵计算中花费的时间是0.3653以获得 135 个员工

新 PLINQ 在非昂贵计算中花费的时间是0.9175来获得 135 名员工

并行性不是免费的,它需要额外的开销。 如果您正在做的工作量非常小,那么间接成本可能会超过您节省的时间。

我强烈推荐阅读免费的 E-Book Patterns of Parallel Programming ,它讨论了您遇到的问题并很好地解释了它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM