Skip to content

Commit

Permalink
Add BigInteger.Multiply benchmark for dotnet/runtime#92208
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Sep 21, 2023
1 parent 11d2879 commit 723814c
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public BigInteger Ctor_ByteArray(BigIntegerData numberString) // the argument na

[Benchmark]
[ArgumentsSource(nameof(NumberStrings))]
public byte[] ToByteArray(BigIntegerData numberString)
public byte[] ToByteArray(BigIntegerData numberString)
=> numberString.Value.ToByteArray();

[Benchmark]
[ArgumentsSource(nameof(NumberStrings))]
public BigInteger Parse(BigIntegerData numberString)
public BigInteger Parse(BigIntegerData numberString)
=> BigInteger.Parse(numberString.Text);

[Benchmark]
Expand Down Expand Up @@ -61,11 +61,6 @@ public BigInteger Add(BigIntegers arguments)
public BigInteger Subtract(BigIntegers arguments)
=> BigInteger.Subtract(arguments.Left, arguments.Right);

[Benchmark]
[ArgumentsSource(nameof(ValuesSameSize))]
public BigInteger Multiply(BigIntegers arguments)
=> BigInteger.Multiply(arguments.Left, arguments.Right);

[Benchmark]
[ArgumentsSource(nameof(ValuesSameSize))]
public BigInteger GreatestCommonDivisor(BigIntegers arguments)
Expand All @@ -78,6 +73,17 @@ public IEnumerable<object> ValuesHalfSize()
yield return new BigIntegers(new[] { 65536, 65536 / 2 });
}

public IEnumerable<object> ValuesSameOrHalfSize()
{
foreach (var item in ValuesSameSize()) yield return item;
foreach (var item in ValuesHalfSize()) yield return item;
}

[Benchmark]
[ArgumentsSource(nameof(ValuesSameOrHalfSize))]
public BigInteger Multiply(BigIntegers arguments)
=> BigInteger.Multiply(arguments.Left, arguments.Right);

[Benchmark]
[ArgumentsSource(nameof(ValuesHalfSize))]
public BigInteger Divide(BigIntegers arguments)
Expand Down Expand Up @@ -115,11 +121,11 @@ public BigIntegerData(string numberString)

public override string ToString() => Text;
}

public class BigIntegers
{
private readonly int[] _bitCounts;

public BigInteger Left { get; }
public BigInteger Right { get; }
public BigInteger Other { get; }
Expand Down Expand Up @@ -159,7 +165,7 @@ private static BigInteger CreateRandomInteger(Random random, int bits)

// ensure actual bit count (remaining bits not set)
// ensure positive value (highest-order bit not set)
value[value.Length - 1] &= (byte) (0xFF >> 8 - bits % 8);
value[value.Length - 1] &= (byte)(0xFF >> 8 - bits % 8);

result = new BigInteger(value);
}
Expand Down

0 comments on commit 723814c

Please sign in to comment.