danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

New interesting results for dynamic result extraction of a generic Task in C#

Daniel WertheimDaniel Wertheim

This is an update to my previous post "Comparing dynamic result extraction of a generic Task in C#" in which I got a comment to extend the benchmarks with more data types as the dynamic call site caching would get affected by it. Yes our course. Good point. So lets have a look.

The test code is of course again BenchmarkDotNet. You can have a look at the benchmark scenarios on GitHub. I'll only show you some pieces in this post.

In this case, I'm just as before, comparing:

but this time, for each case above, it's done for more data types to Task<T>. So each type gets one ICase instance.

public interface ICase  
{
    void ExtractUsingReflection();
    void ExtractUsingReflectionWithSameProp();
    void ExtractUsingReflectionWithCache();
    void ExtractUsingIlWithSameGetter();
    void ExtractUsingIlWithCache();
    void ExtractUsingLambdaWithSameGetter();
    void ExtractUsingLambdaWithCache();
    void ExtractUsingDynamic();
}

The benchmarks are now for the following cases:

_cases = new List<ICase>  
{
    new Case<Thing>(new Thing("Some value")),
    new Case<string>("Some other value"),
    new Case<int>(42),
    new Case<decimal>(3.14M),
    new Case<bool>(true),
    new Case<DateTime>(DateTime.Now),
    new Case<List<int>>(new List<int> { 1, 2, 3, 4, 5 }),
    new Case<string[]>(new []{ ".Net", "C#", "Benchmark" })
};

Results

Dynamic is still the fastest but the more items that are passed the tinier is the difference.

                             Method | Count |            Mean |      StdDev |
----------------------------------- |------ |---------------- |------------ |
                    UsingReflection |     1 |   1,552.6213 ns |   1.5739 ns |
        UsingReflectionWithSameProp |     1 |   1,091.4100 ns |   1.3711 ns |
           UsingReflectionWithCache |     1 |   1,266.5045 ns |   1.3094 ns |
              UsingIlWithSameGetter |     1 |     137.3975 ns |   0.1394 ns |
                   UsingIlWithCache |     1 |     318.8291 ns |   0.1720 ns |
 UsingCompiledLambdasWithSameGetter |     1 |     137.5692 ns |   0.1149 ns |
      UsingCompiledLambdasWithCache |     1 |     318.6473 ns |   0.6646 ns |
                       UsingDynamic |     1 |     289.1129 ns |   1.0529 ns |
                    UsingReflection |    10 |  15,419.8479 ns |  55.7354 ns |
        UsingReflectionWithSameProp |    10 |  10,930.3490 ns |   7.1277 ns |
           UsingReflectionWithCache |    10 |  12,678.2093 ns |  11.3730 ns |
              UsingIlWithSameGetter |    10 |   1,471.3588 ns |  26.0868 ns |
                   UsingIlWithCache |    10 |   3,184.2877 ns |   4.2952 ns |
 UsingCompiledLambdasWithSameGetter |    10 |   1,368.3289 ns |   0.2448 ns |
      UsingCompiledLambdasWithCache |    10 |   3,190.8438 ns |   7.1122 ns |
                       UsingDynamic |    10 |   2,971.2891 ns |   7.6750 ns |
                    UsingReflection |   100 | 152,821.9831 ns |  85.2172 ns |
        UsingReflectionWithSameProp |   100 | 108,606.4911 ns | 108.3779 ns |
           UsingReflectionWithCache |   100 | 126,922.0571 ns | 152.8347 ns |
              UsingIlWithSameGetter |   100 |  13,886.7383 ns |  12.2649 ns |
                   UsingIlWithCache |   100 |  31,770.0469 ns |  38.3124 ns |
 UsingCompiledLambdasWithSameGetter |   100 |  13,753.8686 ns |  22.3101 ns |
      UsingCompiledLambdasWithCache |   100 |  31,903.2993 ns |  15.6638 ns |
                       UsingDynamic |   100 |  28,847.8473 ns |  76.8253 ns |

The end.

//Daniel

Developer that lives by the mantra "code is meant to be shared".

Comments