Performance of expression trees

后端 未结 5 1053
别跟我提以往
别跟我提以往 2020-12-30 23:12

My current understanding is that \'hard coded\' code like this:

public int Add(int x, int y) {return x + y;}

will always perform better tha

5条回答
  •  耶瑟儿~
    2020-12-31 00:05

    Trying to understand why my build and compiled lambda runs slightly slower than "just delegate" (I think I will need create new SO question for it) I've found this thread and have decided to check the performance using BenchmarkDotNet. Surprise for me: there build manually and compiled lambda is quickest. And yes -there is stable difference between methods.

    Results:

    BenchmarkDotNet=v0.10.5, OS=Windows 10.0.14393
    Processor=Intel Core i5-2500K CPU 3.30GHz (Sandy Bridge), ProcessorCount=4
    Frequency=3233539 Hz, Resolution=309.2587 ns, Timer=TSC
      [Host] : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1648.0
      Clr    : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1648.0
      Core   : .NET Core 4.6.25009.03, 64bit RyuJIT
    
    
             Method |  Job | Runtime |      Mean |     Error |    StdDev |    Median |       Min |        Max | Rank | Allocated |
    --------------- |----- |-------- |----------:|----------:|----------:|----------:|----------:|-----------:|-----:|----------:|
         AddBuilded |  Clr |     Clr | 0.8826 ns | 0.0278 ns | 0.0232 ns | 0.8913 ns | 0.8429 ns |  0.9195 ns |    1 |       0 B |
          AddLambda |  Clr |     Clr | 1.5077 ns | 0.0226 ns | 0.0212 ns | 1.4986 ns | 1.4769 ns |  1.5395 ns |    2 |       0 B |
     AddLambdaConst |  Clr |     Clr | 6.4535 ns | 0.0454 ns | 0.0425 ns | 6.4439 ns | 6.4030 ns |  6.5323 ns |    3 |       0 B |
         AddBuilded | Core |    Core | 0.8993 ns | 0.0249 ns | 0.0233 ns | 0.8908 ns | 0.8777 ns |  0.9506 ns |    1 |       0 B |
          AddLambda | Core |    Core | 1.5105 ns | 0.0241 ns | 0.0201 ns | 1.5094 ns | 1.4731 ns |  1.5396 ns |    2 |       0 B |
     AddLambdaConst | Core |    Core | 9.3849 ns | 0.2237 ns | 0.5693 ns | 9.6577 ns | 8.3455 ns | 10.0590 ns |    4 |       0 B |
    

    I can't make any conclusions from this, it can be difference in IL code or JIT compiler impact.

    Code:

        static BenchmarkLambdaSimple()
        {
            addLambda = (a, b) => a + b;
            addLambdaConst = AddMethod;
    
            var x = Expression.Parameter(typeof(int));
            var y = Expression.Parameter(typeof(int));
            var additionExpr = Expression.Add(x, y);
            addBuilded =
                          Expression.Lambda>(
                              additionExpr, x, y).Compile();
        }
        static Func addLambda;
        static Func addLambdaConst;
        static Func addBuilded;
        private static int AddMethod(int a, int b)
        {
            return a + b;
        }
    
        [Benchmark]
        public int AddBuilded()
        {
            return addBuilded(1, 2);
        }
    
        [Benchmark]
        public int AddLambda()
        {
            return addLambda(1, 2);
        }
    
        [Benchmark]
        public int AddLambdaConst()
        {
            return addLambdaConst(1, 2);
        }
    

提交回复
热议问题