Skip to content

Commit

Permalink
Merge pull request #18 from crookseta/2.x
Browse files Browse the repository at this point in the history
2.1
  • Loading branch information
crookseta authored Nov 15, 2024
2 parents 68a8757 + 6465fbf commit c9f689b
Show file tree
Hide file tree
Showing 39 changed files with 2,626 additions and 267 deletions.
132 changes: 47 additions & 85 deletions src/MissingValues.Benchmarks/BigIntegerBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,136 +3,82 @@

namespace MissingValues.Benchmarks;

[MemoryDiagnoser]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
[ShortRunJob]
public class BigIntegerBenchmarks
{
[ParamsSource(nameof(ValuesSource))]
public UInt128 Value { get; set; }
BigInteger b256, b512;
UInt256 u256;
UInt512 u512;

[GlobalSetup]
public void Setup()
{
u256 = (UInt256)(Int256.MaxValue / 2);
u512 = (UInt512)(Int512.MaxValue / 2);

Span<byte> bytes = stackalloc byte[UInt256.Size];
Int256.MaxValue.TryWriteLittleEndian(bytes);

b256 = new BigInteger(bytes, true);

bytes = stackalloc byte[UInt512.Size];
Int512.MaxValue.TryWriteLittleEndian(bytes);

b512 = new BigInteger(bytes, true);
}

[Benchmark]
public BigInteger Add_BigInteger256()
{
return b256 + Value;
}
[Benchmark]
public BigInteger Add_BigInteger512()
{
return b512 + Value;
}
[Benchmark]
public UInt256 Add_UInt256()
[ArgumentsSource(nameof(UInt256ParamsSource))]
public UInt256 Add_UInt256(UInt256 a)
{
return u256 + Value;
return a + Value;
}
[Benchmark]
public UInt512 Add_UInt512()
[ArgumentsSource(nameof(UInt512ParamsSource))]
public UInt512 Add_UInt512(UInt512 a)
{
return u512 + Value;
return a + Value;
}

[Benchmark]
public BigInteger Subtract_BigInteger256()
[ArgumentsSource(nameof(UInt256ParamsSource))]
public UInt256 Subtract_UInt256(UInt256 a)
{
return b256 - Value;
return a - Value;
}
[Benchmark]
public BigInteger Subtract_BigInteger512()
[ArgumentsSource(nameof(UInt512ParamsSource))]
public UInt512 Subtract_UInt512(UInt512 a)
{
return b512 - Value;
}
[Benchmark]
public UInt256 Subtract_UInt256()
{
return u256 - Value;
}
[Benchmark]
public UInt512 Subtract_UInt512()
{
return u512 - Value;
return a - Value;
}

[Benchmark]
public BigInteger Multiply_BigInteger256()
{
return b256 * Value;
}
[Benchmark]
public BigInteger Multiply_BigInteger512()
[ArgumentsSource(nameof(UInt256ParamsSource))]
public UInt256 Multiply_UInt256(UInt256 a)
{
return b512 * Value;
return a * Value;
}
[Benchmark]
public UInt256 Multiply_UInt256()
[ArgumentsSource(nameof(UInt512ParamsSource))]
public UInt512 Multiply_UInt512(UInt512 a)
{
return u256 * Value;
}
[Benchmark]
public UInt512 Multiply_UInt512()
{
return u512 * Value;
return a * Value;
}

[Benchmark]
public BigInteger Divide_BigInteger256()
{
return b256 / Value;
}
[Benchmark]
public BigInteger Divide_BigInteger512()
[ArgumentsSource(nameof(UInt256ParamsSource))]
public UInt256 Divide_UInt256(UInt256 a)
{
return b512 / Value;
return a / Value;
}
[Benchmark]
public UInt256 Divide_UInt256()
[ArgumentsSource(nameof(UInt512ParamsSource))]
public UInt512 Divide_UInt512(UInt512 a)
{
return u256 / Value;
}
[Benchmark]
public UInt512 Divide_UInt512()
{
return u512 / Value;
return a / Value;
}

[Benchmark]
public BigInteger Remainder_BigInteger256()
[ArgumentsSource(nameof(UInt256ParamsSource))]
public UInt256 Remainder_UInt256(UInt256 a)
{
return b256 % Value;
return a % Value;
}
[Benchmark]
public BigInteger Remainder_BigInteger512()
[ArgumentsSource(nameof(UInt512ParamsSource))]
public UInt512 Remainder_UInt512(UInt512 a)
{
return b512 % Value;
}
[Benchmark]
public UInt256 Remainder_UInt256()
{
return u256 % Value;
}
[Benchmark]
public UInt512 Remainder_UInt512()
{
return u512 % Value;
return a % Value;
}

public IEnumerable<UInt128> ValuesSource()
Expand All @@ -142,4 +88,20 @@ public IEnumerable<UInt128> ValuesSource()
yield return UInt128.Parse("10000000000000000000");
yield return UInt128.Parse("100000000000000000000000000000000000000");
}
public IEnumerable<UInt256> UInt256ParamsSource()
{
yield return UInt256.One;
yield return uint.MaxValue;
yield return ulong.MaxValue;
yield return UInt128.MaxValue;
yield return UInt256.MaxValue / UInt256.Parse("10000000000000000000000000000000000000000");
}
public IEnumerable<UInt512> UInt512ParamsSource()
{
yield return UInt512.One;
yield return uint.MaxValue;
yield return ulong.MaxValue;
yield return UInt128.MaxValue;
yield return UInt512.MaxValue / UInt512.Parse("10000000000000000000000000000000000000000");
}
}
Loading

0 comments on commit c9f689b

Please sign in to comment.