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 150f7ba
Showing 1 changed file with 19 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,16 +61,25 @@ 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)
=> BigInteger.GreatestCommonDivisor(arguments.Left, arguments.Right);

public IEnumerable<object> MultiplyValues()
{
foreach (var item in ValuesSameSize()) yield return item;
foreach (var item in ValuesHalfSize()) yield return item;
yield return new BigIntegers(new[] { 16, 16 * 3 / 4 });
yield return new BigIntegers(new[] { 1024, 1024 * 3 / 4 });
yield return new BigIntegers(new[] { 65536, 65536 * 3 / 4 });
}

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

public IEnumerable<object> ValuesHalfSize()
{
yield return new BigIntegers(new[] { 16, 16 / 2 });
Expand Down Expand Up @@ -115,11 +124,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 +168,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 150f7ba

Please sign in to comment.