Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kiminuo committed Oct 23, 2023
1 parent 66d86b4 commit 3d9aad0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
60 changes: 60 additions & 0 deletions NBitcoin.Bench/OutPointBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NBitcoin.Bench
{
public class OutPointBench
{
private const int Count = 10000;
private OutPoint[] _outPoints;

[GlobalSetup]
public void Setup()
{
Random random = new Random(Seed: 53454);

_outPoints = new OutPoint[Count];

for (int i = 0; i < Count; i++)
{
uint256 txid = new uint256(RandomUtils.GetBytes(32));
int n = random.Next(1_000_000) % 3;

_outPoints[i] = new OutPoint(txid, n);
}
}

[Benchmark]
public void Old()
{
_ = DoComparisons(newImplementation: false);
}

[Benchmark]
public void New()
{
_ = DoComparisons(newImplementation: true);
}

private int DoComparisons(bool newImplementation)
{
int count = 0;

foreach (OutPoint o1 in _outPoints)
{
foreach (OutPoint o2 in _outPoints)
{
if (OutPoint.OperatorEqualsEquals(newImplementation, o1, o2))
{
count++;
}
}
}

return count;
}
}
}
22 changes: 22 additions & 0 deletions NBitcoin/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ void SetNull()
return (a.hash == b.hash && a.n == b.n);
}

public static bool OperatorEqualsEquals(bool newImpl, OutPoint? a, OutPoint? b)
{
if (Object.ReferenceEquals(a, null))
{
return Object.ReferenceEquals(b, null);
}
if (Object.ReferenceEquals(b, null))
{
return false;
}

if (newImpl)
{
return (a.hash == b.hash && a.n.CompareTo(b.n) == 0);
}
else
{
return (a.hash == b.hash && a.n == b.n);
}
}

public static bool operator !=(OutPoint? a, OutPoint? b)
{
return !(a == b);
Expand Down Expand Up @@ -206,6 +227,7 @@ public override string ToString()
return $"{Hash}-{N}";
}
}

#nullable disable
public class TxIn : IBitcoinSerializable
{
Expand Down

0 comments on commit 3d9aad0

Please sign in to comment.