From 6849ac2bbd8e683baa6ccb3ad5dbb90fc294b778 Mon Sep 17 00:00:00 2001 From: Igor Machado Coelho Date: Fri, 18 Jan 2019 12:11:16 -0200 Subject: [PATCH] Benchmark structure for UInt classes (#553) * basic benchmark structure for UInt classes * commented code2 from lights for now * updated tests. all seem correct now * Switch to using a benchmark method taking a method delegate to benchmark. * Make pass. * 1 million iterations. * Switch to ulong for the 4th option, and it is still the same speed. * fix test data for 50% equal data * make test pass * neo.UnitTests/UT_UIntBenchmarks.cs * neo.UnitTests/UT_UIntBenchmarks.cs * Base 20 - UInt160 tests * neo.UnitTests/UT_UIntBenchmarks.cs * inlined 160 * complete tests with UInt256 and UInt160 * neo.UnitTests/UT_UIntBenchmarks.cs * Lights division calculation --- neo.UnitTests/UT_UIntBenchmarks.cs | 333 +++++++++++++++++++++++++++++ neo.UnitTests/neo.UnitTests.csproj | 1 + 2 files changed, 334 insertions(+) create mode 100644 neo.UnitTests/UT_UIntBenchmarks.cs 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