From 029d709586fd7a8f734c9f90e198c68b5a3abfbb Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Wed, 16 Jan 2019 13:59:08 -0200 Subject: [PATCH 01/17] basic benchmark structure for UInt classes --- neo.UnitTests/UT_UIntBenchmarks.cs | 137 +++++++++++++++++++++++++++++ neo.UnitTests/neo.UnitTests.csproj | 1 + 2 files changed, 138 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..7d312d809b --- /dev/null +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -0,0 +1,137 @@ +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= 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 = 8; i >= 0; i--) + { + if (lpx[i] > lpy[i]) + return 1; + if (lpx[i] < lpy[i]) + 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 From 58f6ffaea33b13bda8da0a841530a7a4578740bc Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Wed, 16 Jan 2019 14:02:46 -0200 Subject: [PATCH 02/17] commented code2 from lights for now --- neo.UnitTests/UT_UIntBenchmarks.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 7d312d809b..0e40fb899c 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -86,6 +86,7 @@ public void Benchmark_CompareTo() TimeSpan time1 = sw1.Elapsed; Console.WriteLine("Elapsed={0} Sum={1}",time1, checksum1); +/* // testing code2 algorithm Stopwatch sw2 = new Stopwatch(); sw2.Start(); @@ -98,9 +99,10 @@ public void Benchmark_CompareTo() TimeSpan time2 = sw2.Elapsed; Console.WriteLine("Elapsed={0} Sum={1}",time2, checksum2); +*/ checksum0.Should().Be(checksum1); - checksum1.Should().Be(checksum2); + //checksum1.Should().Be(checksum2); } private int code1_UInt256CompareTo(byte[] b1, byte[] b2) @@ -116,7 +118,7 @@ private int code1_UInt256CompareTo(byte[] b1, byte[] b2) } return 0; } - +/* private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) { fixed (byte* px = b1, py = b2) @@ -133,5 +135,6 @@ private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) } return 0; } +*/ } } From 4849d08759d2023ed642024e82d2e7a857509edd Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Wed, 16 Jan 2019 14:44:15 -0200 Subject: [PATCH 03/17] updated tests. all seem correct now --- neo.UnitTests/UT_UIntBenchmarks.cs | 46 ++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 0e40fb899c..6081512d32 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -86,7 +86,7 @@ public void Benchmark_CompareTo() TimeSpan time1 = sw1.Elapsed; Console.WriteLine("Elapsed={0} Sum={1}",time1, checksum1); -/* + // testing code2 algorithm Stopwatch sw2 = new Stopwatch(); sw2.Start(); @@ -99,10 +99,24 @@ public void Benchmark_CompareTo() TimeSpan time2 = sw2.Elapsed; Console.WriteLine("Elapsed={0} Sum={1}",time2, checksum2); -*/ + + // testing code3 algorithm + Stopwatch sw3 = new Stopwatch(); + sw3.Start(); + int checksum3 = 0; + for(var i=0; i= 0; i--) + for (int i = 7; i >= 0; i--) { if (lpx[i] > lpy[i]) return 1; @@ -135,6 +149,26 @@ private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) } return 0; } -*/ + + private unsafe int code3_UInt256CompareTo(byte[] b1, byte[] b2) + { + byte[] xB = b1; + int len = xB.Length >> 2; + fixed (byte* px = xB, py = b2) + { + uint* lpx = (uint*)px+len; + uint* lpy = (uint*)py+len; + uint *lpxStart = (uint*)px; + while (lpx > lpxStart) + { + uint x = *(--lpx); + uint y = *(--lpy); + if (x > y) return 1; + if (y > x) return -1; + } + } + return 0; + } + } } From 33dc8513887280f1cbbe5b547f213a5aa83ba3db Mon Sep 17 00:00:00 2001 From: jsolman Date: Wed, 16 Jan 2019 09:27:56 -0800 Subject: [PATCH 04/17] Switch to using a benchmark method taking a method delegate to benchmark. --- neo.UnitTests/UT_UIntBenchmarks.cs | 98 +++++++++++++++++------------- 1 file changed, 57 insertions(+), 41 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 6081512d32..5e595ae8e6 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -51,6 +51,28 @@ private byte[] RandomBytes(int count) return randomBytes; } + public delegate Object BenchmarkMethod(); + + public (TimeSpan, Object) Benchmark(BenchmarkMethod method) + { + Stopwatch sw0 = new Stopwatch(); + sw0.Start(); + var result = method(); + sw0.Stop(); + TimeSpan elapsed = sw0.Elapsed; + Console.WriteLine($"Elapsed={elapsed} Sum={result}"); + return (elapsed, result); + } + + /* Could do this also so just pass the method to benchmark, but overhead of delegate call might affect benchmark + public delegate int ComparisonMethod(byte[] b1, byte[] b2); + + public int BechmarkComparisonMethod(ComparisonMethod compareMethod) + { + } + */ + + [TestMethod] public void Benchmark_CompareTo() { @@ -63,60 +85,54 @@ public void Benchmark_CompareTo() uut_32_1[i] = new UInt256(base_32_1[i]); uut_32_2[i] = new UInt256(base_32_2[i]); } - Stopwatch sw0 = new Stopwatch(); - sw0.Start(); - int checksum0 = 0; - for(var i=0; i { - checksum1 += code1_UInt256CompareTo(base_32_1[i], base_32_2[i]); - } - sw1.Stop(); - TimeSpan time1 = sw1.Elapsed; - Console.WriteLine("Elapsed={0} Sum={1}",time1, checksum1); + var checksum = 0; + for(var i=0; i { - checksum2 += code2_UInt256CompareTo(base_32_1[i], base_32_2[i]); - } - sw2.Stop(); - TimeSpan time2 = sw2.Elapsed; + var checksum = 0; + for(var i=0; i { - checksum3 += code3_UInt256CompareTo(base_32_1[i], base_32_2[i]); - } - sw3.Stop(); - TimeSpan time3 = sw3.Elapsed; + var checksum = 0; + for(var i=0; i + { + var checksum = 0; + for(var i=0; i Date: Wed, 16 Jan 2019 09:34:20 -0800 Subject: [PATCH 05/17] Make pass. --- neo.UnitTests/UT_UIntBenchmarks.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 5e595ae8e6..b1b4f8321f 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -15,7 +15,7 @@ namespace Neo.UnitTests [TestClass] public class UT_UIntBenchmarks { - int MAX_TESTS = 1000000; // 1 million + int MAX_TESTS = 10000000; // 1 million byte[][] base_32_1; byte[][] base_32_2; @@ -132,7 +132,7 @@ public void Benchmark_CompareTo() checksum0.Should().Be(checksum1); checksum0.Should().Be(checksum2); - checksum0.Should().Be(0); + checksum0.Should().Be(checksum3); } private int code1_UInt256CompareTo(byte[] b1, byte[] b2) From cb1f6dabbad193404e00b6efd32562a2430189e3 Mon Sep 17 00:00:00 2001 From: jsolman Date: Wed, 16 Jan 2019 09:36:49 -0800 Subject: [PATCH 06/17] 1 million iterations. --- neo.UnitTests/UT_UIntBenchmarks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index b1b4f8321f..4a3e529fef 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -15,7 +15,7 @@ namespace Neo.UnitTests [TestClass] public class UT_UIntBenchmarks { - int MAX_TESTS = 10000000; // 1 million + int MAX_TESTS = 1000000; // 1 million byte[][] base_32_1; byte[][] base_32_2; From 7906691bc5134ac4b068fcd435e3e9a4e4b9458b Mon Sep 17 00:00:00 2001 From: jsolman Date: Thu, 17 Jan 2019 00:41:57 -0800 Subject: [PATCH 07/17] Switch to ulong for the 4th option, and it is still the same speed. --- neo.UnitTests/UT_UIntBenchmarks.cs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 4a3e529fef..eb396a54d9 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -15,7 +15,7 @@ namespace Neo.UnitTests [TestClass] public class UT_UIntBenchmarks { - int MAX_TESTS = 1000000; // 1 million + int MAX_TESTS = 10000000; // 1 million byte[][] base_32_1; byte[][] base_32_2; @@ -132,7 +132,7 @@ public void Benchmark_CompareTo() checksum0.Should().Be(checksum1); checksum0.Should().Be(checksum2); - checksum0.Should().Be(checksum3); + checksum0.Should().Be(0); } private int code1_UInt256CompareTo(byte[] b1, byte[] b2) @@ -168,19 +168,16 @@ private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) private unsafe int code3_UInt256CompareTo(byte[] b1, byte[] b2) { - byte[] xB = b1; - int len = xB.Length >> 2; - fixed (byte* px = xB, py = b2) + fixed (byte* px = b1, py = b2) { - uint* lpx = (uint*)px+len; - uint* lpy = (uint*)py+len; - uint *lpxStart = (uint*)px; - while (lpx > lpxStart) + ulong* lpx = (ulong*)px; + ulong* lpy = (ulong*)py; + for (int i = 3; i >= 0; i--) { - uint x = *(--lpx); - uint y = *(--lpy); - if (x > y) return 1; - if (y > x) return -1; + if (lpx[i] > lpy[i]) + return 1; + if (lpx[i] < lpy[i]) + return -1; } } return 0; From d0377a45df2026757ad41cbdf65c43c090f6d844 Mon Sep 17 00:00:00 2001 From: lights Date: Thu, 17 Jan 2019 18:40:30 +0800 Subject: [PATCH 08/17] fix test data for 50% equal data --- neo.UnitTests/UT_UIntBenchmarks.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index eb396a54d9..7ce85683ac 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -38,7 +38,15 @@ public void TestSetup() for(var i=0; i Date: Thu, 17 Jan 2019 18:55:39 +0800 Subject: [PATCH 09/17] make test pass --- neo.UnitTests/UT_UIntBenchmarks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 7ce85683ac..725d0eb70b 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -140,7 +140,7 @@ public void Benchmark_CompareTo() checksum0.Should().Be(checksum1); checksum0.Should().Be(checksum2); - checksum0.Should().Be(0); + checksum0.Should().Be(checksum3); } private int code1_UInt256CompareTo(byte[] b1, byte[] b2) From e2e506b4698db674b24b5de2bdb3c5421ed66bde Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Thu, 17 Jan 2019 11:04:16 -0200 Subject: [PATCH 10/17] neo.UnitTests/UT_UIntBenchmarks.cs --- neo.UnitTests/UT_UIntBenchmarks.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 725d0eb70b..870af66600 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -15,7 +15,7 @@ namespace Neo.UnitTests [TestClass] public class UT_UIntBenchmarks { - int MAX_TESTS = 10000000; // 1 million + int MAX_TESTS = 1000000; // 1 million byte[][] base_32_1; byte[][] base_32_2; @@ -141,6 +141,7 @@ public void Benchmark_CompareTo() checksum0.Should().Be(checksum1); checksum0.Should().Be(checksum2); checksum0.Should().Be(checksum3); + checksum0.Should().Be(0); } private int code1_UInt256CompareTo(byte[] b1, byte[] b2) From ef72646a7f193a389981ce796c16322ab6e95b94 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Thu, 17 Jan 2019 11:10:38 -0200 Subject: [PATCH 11/17] neo.UnitTests/UT_UIntBenchmarks.cs --- neo.UnitTests/UT_UIntBenchmarks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 870af66600..14321aed8d 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -82,7 +82,7 @@ public int BechmarkComparisonMethod(ComparisonMethod compareMethod) [TestMethod] - public void Benchmark_CompareTo() + public void Benchmark_CompareTo_UInt256() { // testing "official version" UInt256[] uut_32_1 = new UInt256[MAX_TESTS]; From f57f400d6d5427c00e61922a7e20b60b83f2a10f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Thu, 17 Jan 2019 11:14:58 -0200 Subject: [PATCH 12/17] Base 20 - UInt160 tests --- neo.UnitTests/UT_UIntBenchmarks.cs | 111 ++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 14321aed8d..a2374a06b1 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -80,7 +80,6 @@ public int BechmarkComparisonMethod(ComparisonMethod compareMethod) } */ - [TestMethod] public void Benchmark_CompareTo_UInt256() { @@ -144,6 +143,69 @@ public void Benchmark_CompareTo_UInt256() checksum0.Should().Be(0); } + [TestMethod] + public void Benchmark_CompareTo_UInt160() + { + // testing "official version" + UInt256[] uut_20_1 = new UInt160[MAX_TESTS]; + UInt256[] uut_20_2 = new UInt160[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= 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 = 4; 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) + { + fixed (byte* px = b1, py = b2) + { + ulong* lpx = (ulong*)px; + ulong* lpy = (ulong*)py; + for (int i = 2; i >= 0; i--) + { + if (lpx[i] > lpy[i]) + return 1; + if (lpx[i] < lpy[i]) + return -1; + } + } + return 0; + } } } From 3ac663ae55b0af272713fc2ec6352995178df52c Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Thu, 17 Jan 2019 11:19:21 -0200 Subject: [PATCH 13/17] neo.UnitTests/UT_UIntBenchmarks.cs --- neo.UnitTests/UT_UIntBenchmarks.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index a2374a06b1..516da0e223 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -38,17 +38,19 @@ public void TestSetup() for(var i=0; i Date: Thu, 17 Jan 2019 12:05:59 -0200 Subject: [PATCH 14/17] inlined 160 --- neo.UnitTests/UT_UIntBenchmarks.cs | 33 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 516da0e223..bc8414d4fd 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -28,7 +28,7 @@ public class UT_UIntBenchmarks public void TestSetup() { int SEED = 123456789; - random = new Random(SEED); + random = new Random();//new Random(SEED); base_32_1 = new byte[MAX_TESTS][]; base_32_2 = new byte[MAX_TESTS][]; @@ -85,7 +85,7 @@ public int BechmarkComparisonMethod(ComparisonMethod compareMethod) [TestMethod] public void Benchmark_CompareTo_UInt256() { - // testing "official version" + // testing "official UInt256 version" UInt256[] uut_32_1 = new UInt256[MAX_TESTS]; UInt256[] uut_32_2 = new UInt256[MAX_TESTS]; @@ -148,7 +148,7 @@ public void Benchmark_CompareTo_UInt256() [TestMethod] public void Benchmark_CompareTo_UInt160() { - // testing "official version" + // testing "official UInt160 version" UInt160[] uut_20_1 = new UInt160[MAX_TESTS]; UInt160[] uut_20_2 = new UInt160[MAX_TESTS]; @@ -288,17 +288,30 @@ private unsafe int code2_UInt160CompareTo(byte[] b1, byte[] b2) private unsafe int code3_UInt160CompareTo(byte[] b1, byte[] b2) { + // -------------------------- + // | 8B | 8B | 4B | + // -------------------------- + // 0l 1l 4i + // -------------------------- fixed (byte* px = b1, py = b2) { + uint* lpxi = (uint*)px; + uint* lpyi = (uint*)py; + if (lpxi[4] > lpyi[4]) + return 1; + if (lpxi[4] < lpyi[4]) + return -1; + ulong* lpx = (ulong*)px; ulong* lpy = (ulong*)py; - for (int i = 2; i >= 0; i--) - { - if (lpx[i] > lpy[i]) - return 1; - if (lpx[i] < lpy[i]) - return -1; - } + 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; } From 4622e775aeec20534a61f5b7fc9b71323284bfb6 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Thu, 17 Jan 2019 12:16:54 -0200 Subject: [PATCH 15/17] complete tests with UInt256 and UInt160 --- neo.UnitTests/UT_UIntBenchmarks.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index bc8414d4fd..f12f343442 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -28,7 +28,7 @@ public class UT_UIntBenchmarks public void TestSetup() { int SEED = 123456789; - random = new Random();//new Random(SEED); + random = new Random(SEED); base_32_1 = new byte[MAX_TESTS][]; base_32_2 = new byte[MAX_TESTS][]; @@ -142,7 +142,6 @@ public void Benchmark_CompareTo_UInt256() checksum0.Should().Be(checksum1); checksum0.Should().Be(checksum2); checksum0.Should().Be(checksum3); - checksum0.Should().Be(0); } [TestMethod] @@ -205,7 +204,20 @@ public void Benchmark_CompareTo_UInt160() checksum0.Should().Be(checksum1); checksum0.Should().Be(checksum2); checksum0.Should().Be(checksum3); - checksum0.Should().Be(0); + } + + [TestMethod] + public void Benchmark_UInt_IsCorrect_Self_CompareTo() + { + for(var i=0; i Date: Thu, 17 Jan 2019 12:33:18 -0200 Subject: [PATCH 16/17] neo.UnitTests/UT_UIntBenchmarks.cs --- neo.UnitTests/UT_UIntBenchmarks.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index f12f343442..8a750e79ff 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -300,6 +300,7 @@ private unsafe int code2_UInt160CompareTo(byte[] b1, byte[] b2) private unsafe int code3_UInt160CompareTo(byte[] b1, byte[] b2) { + // LSB -----------------> MSB // -------------------------- // | 8B | 8B | 4B | // -------------------------- @@ -307,11 +308,11 @@ private unsafe int code3_UInt160CompareTo(byte[] b1, byte[] b2) // -------------------------- fixed (byte* px = b1, py = b2) { - uint* lpxi = (uint*)px; - uint* lpyi = (uint*)py; - if (lpxi[4] > lpyi[4]) + uint* ipx = (uint*)px; + uint* ipy = (uint*)py; + if (ipx[4] > ipy[4]) return 1; - if (lpxi[4] < lpyi[4]) + if (ipx[4] < ipy[4]) return -1; ulong* lpx = (ulong*)px; From c0456d9df4a6af2f72a51a505be645358335c677 Mon Sep 17 00:00:00 2001 From: Igor Machado Coelho Date: Thu, 17 Jan 2019 16:02:57 -0200 Subject: [PATCH 17/17] Lights division calculation --- neo.UnitTests/UT_UIntBenchmarks.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/neo.UnitTests/UT_UIntBenchmarks.cs b/neo.UnitTests/UT_UIntBenchmarks.cs index 8a750e79ff..1c388354a5 100644 --- a/neo.UnitTests/UT_UIntBenchmarks.cs +++ b/neo.UnitTests/UT_UIntBenchmarks.cs @@ -240,7 +240,7 @@ private unsafe int code2_UInt256CompareTo(byte[] b1, byte[] b2) { uint* lpx = (uint*)px; uint* lpy = (uint*)py; - for (int i = 7; i >= 0; i--) + for (int i = 256/32-1; i >= 0; i--) { if (lpx[i] > lpy[i]) return 1; @@ -257,7 +257,7 @@ private unsafe int code3_UInt256CompareTo(byte[] b1, byte[] b2) { ulong* lpx = (ulong*)px; ulong* lpy = (ulong*)py; - for (int i = 3; i >= 0; i--) + for (int i = 256/64-1; i >= 0; i--) { if (lpx[i] > lpy[i]) return 1; @@ -287,7 +287,7 @@ private unsafe int code2_UInt160CompareTo(byte[] b1, byte[] b2) { uint* lpx = (uint*)px; uint* lpy = (uint*)py; - for (int i = 4; i >= 0; i--) + for (int i = 160/32-1; i >= 0; i--) { if (lpx[i] > lpy[i]) return 1;