In this approach, we'll find out how to most efficiently calculate the value for Grains in C#.
The approaches page lists two idiomatic approaches to this exercise:
Benchmarks
To benchmark the approaches, we wrote a small benchmark application using BenchmarkDotNet library.
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|---|---|---|---|---|---|
| TotalWithRange | 2,060.88 ns | 21.030 ns | 17.561 ns | 0.0191 | 104 B |
| TotalByBits | 59.15 ns | 0.446 ns | 0.417 ns | 0.0237 | 112 B |
Enumerating a Range from 1 to 64 to sum each call of Square (which uses Pow) is about thirty-five times slower than using bit shifting on BigInteger.
4th Sep 2024
·
Found it useful?