Ad
  • Default User Avatar

    Hello folks ...
    Well who ever spoke of performance is absolutely right ...
    The "classic" switch will do the job much more efficiently than any other implementation version.
    I like the elegacy of the code above also but performance wise is nowhere near the simple "switch" statement solution.
    I run a benchmark test (using the BenchmarkDotNet v0.13.7 package) for the above (renamed as AmIAfraid02) implementation.
    The last one (AmIAfraid03) is the one using the "switch" statement.

    See the summary for yourselves:

    // * Summary *

    BenchmarkDotNet v0.13.7, Windows 10 (10.0.19044.3086/21H2/November2021Update)
    Intel Core i5-7500 CPU 3.40GHz (Kaby Lake), 1 CPU, 4 logical and 4 physical cores
    .NET SDK 7.0.400
    [Host] : .NET 6.0.21 (6.0.2123.36311), X64 RyuJIT AVX2
    .NET 7.0 : .NET 7.0.10 (7.0.1023.36312), X64 RyuJIT AVX2

    Job=.NET 7.0 Runtime=.NET 7.0

    Method Mean Error StdDev Ratio RatioSD
    AmIAfraid01 2,045.066 ns 10.7679 ns 10.0723 ns 989.74 21.65
    AmIAfraid02 207.748 ns 0.7933 ns 0.6194 ns 101.37 1.33
    AmIAfraid03 2.067 ns 0.0465 ns 0.0435 ns 1.00 0.00

    // * Hints *
    Outliers
    BenchmarkAmIAfraidMethod.AmIAfraid02: .NET 7.0 -> 4 outliers were removed (218.49 ns..220.67 ns)
    // * Config Issues *

    // * Warnings *
    Configuration
    Summary -> The exporter BenchmarkDotNet.Exporters.MarkdownExporter is already present in configuration. There may be unexpected results.
    Summary -> The exporter BenchmarkDotNet.Exporters.HtmlExporter is already present in configuration. There may be unexpected results.
    Summary -> The exporter BenchmarkDotNet.Exporters.Csv.CsvExporter is already present in configuration. There may be unexpected results.

    // * Legends *
    Mean : Arithmetic mean of all measurements
    Error : Half of 99.9% confidence interval
    StdDev : Standard deviation of all measurements
    Ratio : Mean of the ratio distribution ([Current]/[Baseline])
    RatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline])
    1 ns : 1 Nanosecond (0.000000001 sec)

    // ***** BenchmarkRunner: End *****
    Run time: 00:00:59 (59.8 sec), executed benchmarks: 3

    Global total time: 00:01:05 (65.47 sec), executed benchmarks: 3
    // * Artifacts cleanup *
    Artifacts cleanup is finished

  • Custom User Avatar

    This still takes much more memory and CPU to process than a simple switch statement. It looks cool, but is not that clever.

  • Default User Avatar

    No; it's easy to make that mistake on first glance, but a closer look reveals that the dictionary contains a series of lambda functions. Only the one selected by the key (day) is actually then executed.