Skip to content

Instantly share code, notes, and snippets.

@martincostello
Created August 16, 2024 16:32
Show Gist options
  • Save martincostello/3422070932926e5d11eb757fca2b7765 to your computer and use it in GitHub Desktop.
Save martincostello/3422070932926e5d11eb757fca2b7765 to your computer and use it in GitHub Desktop.
BigInteger go brrr in .NET 9
using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
namespace DotNetBenchmarks;
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net80, baseline: true)]
[SimpleJob(RuntimeMoniker.Net90)]
public class BigIntegerBenchmarks
{
[Benchmark]
public long ProjectEuler97()
{
BigInteger value = (28433 * BigInteger.Pow(2, 7830457)) + 1;
var digits = new List<int>(10);
for (int i = 0; i < 10; i++)
{
value = BigInteger.DivRem(value, 10, out var rem);
digits.Add((int)rem);
}
digits.Reverse();
return FromDigits(digits);
}
private static long FromDigits(List<int> collection)
{
double result = 0;
for (int j = 0; j < collection.Count - 1; j++)
{
result += collection[j] * Math.Pow(10, collection.Count - j - 1);
}
result += collection[collection.Count - 1];
return (long)result;
}
}

BenchmarkDotNet v0.14.0, Windows 11 (10.0.22621.4037/22H2/2022Update/SunValley2)
12th Gen Intel Core i7-1270P, 1 CPU, 16 logical and 12 physical cores
.NET SDK 9.0.100-preview.7.24407.12
  [Host]   : .NET 9.0.0 (9.0.24.40507), X64 RyuJIT AVX2
  .NET 8.0 : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.40507), X64 RyuJIT AVX2


Method Job Runtime Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
ProjectEuler97 .NET 8.0 .NET 8.0 4,695.3 ms 138.27 ms 405.53 ms 1.01 0.12 12.14 MB 1.00
ProjectEuler97 .NET 9.0 .NET 9.0 967.4 ms 19.07 ms 20.40 ms 0.21 0.02 12.14 MB 1.00
@martincostello
Copy link
Author

.NET 8 Speedscope

image

.NET 9 Speedscope

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment