按类型划分Linq查询的性能

Øyv*_*ind 5 .net c# linq performance

讨论已经开始起作用:

我们有一个有IList的课程.事实是一个抽象的基类,有几个具体的子类(PopulationFact,GdpFact等).

最初我们以这种方式查询给定的事实,即按类型:

.Facts.FirstOrDefault(x => x.Year == 2011 && x is GdpFact)
Run Code Online (Sandbox Code Playgroud)

然而,现在提出的问题是我们是否应该引入FactType枚举来代替

.Facts.FirstOrDefault(x => x.Year == 2011 && x.FactType == FactType.Gdp)
Run Code Online (Sandbox Code Playgroud)

提出这个建议是因为它应该更快.我承认我没有写任何测试来试图辨别性能上的差异,但我有两个问题:

1)这样的'查询类型'本身就是坏的吗?
2)在事实​​是强类型的情况下,是不是添加FactType枚举只是多余的?

更新 为了澄清,这是LINQ to objects和GdpFact:Fact.

更新2 我们使用当前的典型数据(4个事实)进行了测量,结果如下:

在枚举上查找:在类型上查找0.296600000000000000003毫秒:0.24530000000000002毫秒

因此在这种情况下类型查找更快!我会仔细选择我接受的答案.

Jod*_*ell 3

我做了一个测试,1000000次迭代的结果大约是

ByCast 166ms
ByType 84ms
ByEnum 98ms
Run Code Online (Sandbox Code Playgroud)

因此,enum实际上是多余的,而且速度较慢,但​​速度并不慢。这应该不会太令人惊讶,类型系统是 .Net Framework 的基础。

测试代码转录如下,对勘误表表示歉意

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

class Program
{
    private enum TypeOfFact
    {
        Gdp,
        Other
    }

    private abstract class Fact
    {
        public virtual int Year { get; set; }
        public abstract TypeOfFact FactType { get; }
    }

    private class GdpFact : Fact
    {
        public override TypeOfFact FactType
        {
            get { return TypeOfFact.Gdp; }
        }
    }

    private class OtherFact : Fact
    {
        public override TypeOfFact FactType
        {
            get { return TypeOfFact.Other; }
        }
    }

    static void Main()
    {
        Ilist<Fact> facts = new List<Fact>
            {
                new GdpFact { Year = 2010 },
                new OtherFact { Year = 2010 },
                new GdpFact { Year = 2009 },
                new OtherFact { Year = 2009 },
                new GdpFact { Year = 2011 },
                new OtherFact { Year = 2011 },
            };

        const int interations = 1000000;

        var funcs = new List<Func<IList<Fact>, Fact>>
            {
                ByList,
                ByType,
                ByEnum
            };

        // Warmup
        foreach (var func in funcs)
        {
           Measure(5, func, facts);
        }

        // Results
        foreach (var result in funcs.Select(f => new
            {
                Description = f.Method.Name,
                Ms = Measure(iterations, f, facts)
            }))
        {
            Console.WriteLine(
                "{0} time = {1}ms",
                result.Description,
                result.Ms);
        }
    }

    private static long Measure(
        int iterations,
        Func<IList<Fact>, Fact> func,
        IList<Fact> facts)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        for (var i = 0; i < iterations; i++)
        {
            func.Invoke(facts);
        }

        stopwatch.Stop();
        return stopwatch.ElapsedMilliseconds;
    }

    private static Fact ByType(IList<Fact> facts)
    {
        return facts.FirstOrDefault(f =>
            f.Year == 2011 && f is GdpFact);
    }

    private static Fact ByEnum(IList<Fact> facts)
    {
        return facts.FirstOrDefault(f =>
            f.Year == 2011 && f.FactType == TypeOfFact.Gdp);
    }

    private static Fact ByCast(IList<Fact> facts)
    {
        return facts.OfType<GdpFact>()
            .FirstOrDefault(f => f.Year == 2011);
    }
}
Run Code Online (Sandbox Code Playgroud)

这个问题似乎很相关。