diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs new file mode 100644 index 0000000000..1c388354a5 --- /dev/null +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -0,0 +1,333 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Cryptography.ECC; +using Neo.IO; +using Neo.Ledger; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Diagnostics; +using System; +//using System.Runtime.CompilerServices.Unsafe; + +namespace Neo.UnitTests +{ + [TestClass] + public class UT_UIntBenchmarks + { + int MAX_TESTS = 1000000; // 1 million + + byte[][] base_32_1; + byte[][] base_32_2; + byte[][] base_20_1; + byte[][] base_20_2; + + private Random random; + + [TestInitialize] + public void TestSetup() + { + int SEED = 123456789; + random = new Random(SEED); + + base_32_1 = new byte[MAX_TESTS][]; + base_32_2 = new byte[MAX_TESTS][]; + base_20_1 = new byte[MAX_TESTS][]; + base_20_2 = new byte[MAX_TESTS][]; + + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i= 0; i--) + { + if (x[i] > y[i]) + return 1; + if (x[i] < y[i]) + return -1; + } + return 0; + } + + private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) + { + fixed (byte* px = b1, py = b2) + { + uint* lpx = (uint*)px; + uint* lpy = (uint*)py; + for (int i = 256/32-1; i >= 0; i--) + { + if (lpx[i] > lpy[i]) + return 1; + if (lpx[i] < lpy[i]) + return -1; + } + } + return 0; + } + + private unsafe int code3_UInt256CompareTo(byte[] b1, byte[] b2) + { + fixed (byte* px = b1, py = b2) + { + ulong* lpx = (ulong*)px; + ulong* lpy = (ulong*)py; + for (int i = 256/64-1; i >= 0; i--) + { + if (lpx[i] > lpy[i]) + return 1; + if (lpx[i] < lpy[i]) + return -1; + } + } + return 0; + } + private int code1_UInt160CompareTo(byte[] b1, byte[] b2) + { + byte[] x = b1; + byte[] y = b2; + for (int i = x.Length - 1; i >= 0; i--) + { + if (x[i] > y[i]) + return 1; + if (x[i] < y[i]) + return -1; + } + return 0; + } + + private unsafe int code2_UInt160CompareTo(byte[] b1, byte[] b2) + { + fixed (byte* px = b1, py = b2) + { + uint* lpx = (uint*)px; + uint* lpy = (uint*)py; + for (int i = 160/32-1; i >= 0; i--) + { + if (lpx[i] > lpy[i]) + return 1; + if (lpx[i] < lpy[i]) + return -1; + } + } + return 0; + } + + private unsafe int code3_UInt160CompareTo(byte[] b1, byte[] b2) + { + // LSB -----------------> MSB + // -------------------------- + // | 8B | 8B | 4B | + // -------------------------- + // 0l 1l 4i + // -------------------------- + fixed (byte* px = b1, py = b2) + { + uint* ipx = (uint*)px; + uint* ipy = (uint*)py; + if (ipx[4] > ipy[4]) + return 1; + if (ipx[4] < ipy[4]) + return -1; + + ulong* lpx = (ulong*)px; + ulong* lpy = (ulong*)py; + if (lpx[1] > lpy[1]) + return 1; + if (lpx[1] < lpy[1]) + return -1; + if (lpx[0] > lpy[0]) + return 1; + if (lpx[0] < lpy[0]) + return -1; + } + return 0; + } + + } +} diff --git a/neo.UnitTests/neo.UnitTests.csproj b/neo.UnitTests/neo.UnitTests.csproj index 3d75fd8e28..158bfba690 100644 --- a/neo.UnitTests/neo.UnitTests.csproj +++ b/neo.UnitTests/neo.UnitTests.csproj @@ -5,6 +5,7 @@ netcoreapp2.0 Neo.UnitTests Neo.UnitTests + true