Code Monkey home page Code Monkey logo

sourcegeneratortest's Introduction

Testing C# 9 Source Generators

Instead of using reflection to get an Attribute from an enum member, I tried to use C# 9 Source Generators.

The attribute is simply put atop an enum member like so:

public enum Stuff {
  [SomeString("๐Ÿฏ")]
  Honey,
  [SomeString("๐Ÿฆ™")]
  Llama,
  ...
}

and can be retrieved via extension method:

Stuff.Llama.ToSomeString();

The result is - as expected - much more performant.

Benchmark Output

// * Summary *

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.18363.1679 (1909/November2019Update/19H2)
Intel Core i7-10610U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK=5.0.301
  [Host]     : .NET 5.0.7 (5.0.721.25508), X64 RyuJIT
  DefaultJob : .NET 5.0.7 (5.0.721.25508), X64 RyuJIT


|               Method |         Mean |      Error |     StdDev |  Gen 0 | Allocated |
|--------------------- |-------------:|-----------:|-----------:|-------:|----------:|
|       WithReflection | 1,229.783 ns | 21.2446 ns | 19.8722 ns | 0.0687 |     288 B |
| WithSourceGeneration |     4.343 ns |  0.1492 ns |  0.1887 ns | 0.0057 |      24 B |

// * Hints *
Outliers
  ToEmojiBenchmark.WithReflection: Default -> 1 outlier  was  detected (1.18 us)

// * Legends *
  Mean      : Arithmetic mean of all measurements
  Error     : Half of 99.9% confidence interval
  StdDev    : Standard deviation of all measurements
  Gen 0     : GC Generation 0 collects per 1000 operations
  Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
  1 ns      : 1 Nanosecond (0.000000001 sec)

sourcegeneratortest's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.