https://github.com/chrishoy/benchmarksbasicsdotnet9
Simple benchmarking, DotNet 9 using BenchmarkDotnet
https://github.com/chrishoy/benchmarksbasicsdotnet9
benchmarkdotnet benchmarking csharp
Last synced: about 1 year ago
JSON representation
Simple benchmarking, DotNet 9 using BenchmarkDotnet
- Host: GitHub
- URL: https://github.com/chrishoy/benchmarksbasicsdotnet9
- Owner: chrishoy
- License: mit
- Created: 2025-04-10T09:54:20.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-10T12:42:28.000Z (about 1 year ago)
- Last Synced: 2025-04-13T01:06:09.188Z (about 1 year ago)
- Topics: benchmarkdotnet, benchmarking, csharp
- Language: C#
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Simple .Net 9 Benchmark Comparisons
I was asked recently how to convert an integer representing a month to the quarter the month is in.
Turns out the question was about performance, and my simple answer, use a simple `(month - 1) / 3 + 1`,
which uses integer division and therefore should be very fast anyway, was not quite what was being looked for.
When pressed further I mentioned you could use a switch expression, but what they were looking for was
to use a static array and do a simple lookup.
So on my way back home I thought about this, and decided to do a benchmark comparison.
Always good to prove simple things like this for yourself :wink:
## How to Use
- Clone the repository
- Open the solution in `VS Code`
- `launch.json` and `tasks.json` are pre-configured to run the benchmarks
- Run and see the results in the Terminal window
## Results
And here's the proof!
| Method | Mean | Error | StdDev | Median | Allocated |
|------------------------------------ |----------:|----------:|----------:|----------:|----------:|
| MonthToQuarterUsingArray | 8.562 ns | 0.2031 ns | 0.5278 ns | 8.456 ns | - |
| MonthToQuarterUsingIntegerDivision | 13.048 ns | 0.2925 ns | 0.3251 ns | 12.963 ns | - |
| MonthToQuarterUsingSwitchExpression | 32.312 ns | 0.7340 ns | 2.0940 ns | 31.955 ns | - |
| MonthToQuarterUsingSwitchStatement | 28.136 ns | 0.9185 ns | 2.6646 ns | 27.422 ns | - |