Skip to content

Commit 95c3402

Browse files
committed
Add bench
1 parent 5515ad4 commit 95c3402

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎NBitcoin.Bench/UtilsBench.cs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using BenchmarkDotNet.Attributes;
2+
using NBitcoin.DataEncoders;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace NBitcoin.Bench;
7+
8+
[MemoryDiagnoser]
9+
public class UtilsBench
10+
{
11+
private static readonly byte[] taprootPubKeyA = Encoders.Hex.DecodeData("54c9e50dde41d5fc0b3dc0ff1ed4f6603a493d0a50dcc1badc3a5d959da7e750");
12+
private static readonly byte[] taprootPubKeyB = Encoders.Hex.DecodeData("7ca556cf2fc5ba848c5ede3c8f0605dc78c005244915ad42b4b15076eb4b2c43");
13+
14+
public static IEnumerable<object[]> ArrayEqualArgs
15+
{
16+
get
17+
{
18+
yield return new byte[][] { [], [] }; // No bytes.
19+
yield return new byte[][] { [], [0x01] }; // Empty array against non-empty array.
20+
yield return new byte[][] { [0x01], [0x02] }; // 1-byte arrays which are not equal.
21+
yield return new byte[][] { [0x01, 0x02], [0x01, 0x03] }; // 2-byte arrays which are not equal.
22+
yield return new byte[][] { [0x01, 0x02, 0x03], [0x02, 0x03, 0x04] }; // 3-byte arrays which are not equal.
23+
yield return new byte[][] { [0x01, 0x02, 0x03, 0x04, 0x05], [0x01, 0x02, 0x03, 0x04, 0x05] }; // 5-byte arrays which are equal.
24+
yield return new byte[][] { [0x01, 0x02, 0x03, 0x04, 0x06, 0x05], [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] }; // 6-byte arrays which are not equal.
25+
yield return new byte[][] { [.. taprootPubKeyA], [.. taprootPubKeyA] }; // Create a copy to avoid reference equality
26+
yield return new byte[][] { [.. taprootPubKeyA], [.. taprootPubKeyB] };
27+
}
28+
}
29+
30+
[Benchmark(Baseline = true)]
31+
[BenchmarkCategory("Master")]
32+
[ArgumentsSource(nameof(ArrayEqualArgs))]
33+
public bool ArrayEqual_Master(byte[] a, byte[] b)
34+
{
35+
return ArrayEqual_OldImpl(a, b);
36+
}
37+
38+
[Benchmark]
39+
[ArgumentsSource(nameof(ArrayEqualArgs))]
40+
public bool ArrayEqual(byte[] a, byte[] b)
41+
{
42+
return Utils.ArrayEqual(a, b);
43+
}
44+
45+
private static bool ArrayEqual_OldImpl(byte[] a, byte[] b)
46+
{
47+
if (a == null && b == null)
48+
return true;
49+
if (a == null)
50+
return false;
51+
if (b == null)
52+
return false;
53+
54+
return Utils.ArrayEqual(a, 0, b, 0, Math.Max(a.Length, b.Length));
55+
}
56+
}

0 commit comments

Comments
 (0)