C# Micro Optimizations Part 2 – In Arguments
Exploring C# Micro Optimizations Part 2 – In Arguments
Follow the accompanying blog post here - https://albertherd.com/2019/06/20/c-micro-optimizations-part-1-ref-arguments/
Exploring overcoming the mutability issue presented using the ref keyword by using in parameter modifier with readonly structs
When accessing 2 properties in a struct, the performance difference between a ref method and an in method without having a read-only struct, performance is around 10 times slower.
Method | limit | Mean | Error | StdDev |
---|---|---|---|---|
BenchmarkIncrementByRef | 100000000 | 23.83 ms | 0.0272 ms | 0.0241 ms |
BenchmarkIncrementByIn | 100000000 | 238.21 ms | 0.3108 ms | 0.2755 ms |
When switching the struct to read-only, the performance hit is not evident anymore
Method | limit | Mean | Error | StdDev |
---|---|---|---|---|
BenchmarkIncrementByRef | 100000000 | 23.93 ms | 0.1226 ms | 0.1147 ms |
BenchmarkIncrementByIn | 100000000 | 24.06 ms | 0.2183 ms | 0.2042 ms |