Skip to content
C# Micro Optimizations Part 2 – In Arguments
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
InOperator
InOperator.sln
README.md

README.md

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.

MethodlimitMeanErrorStdDev
BenchmarkIncrementByRef10000000023.83 ms0.0272 ms0.0241 ms
BenchmarkIncrementByIn100000000238.21 ms0.3108 ms0.2755 ms

When switching the struct to read-only, the performance hit is not evident anymore

MethodlimitMeanErrorStdDev
BenchmarkIncrementByRef10000000023.93 ms0.1226 ms0.1147 ms
BenchmarkIncrementByIn10000000024.06 ms0.2183 ms0.2042 ms
You can’t perform that action at this time.