Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark structure for UInt classes #553

Merged
merged 20 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions neo.UnitTests/UT_UIntBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
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 = 10000000; // 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<MAX_TESTS; i++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igormcoelho did you calculate the amount of RAM used for this test? is not excessive?

{
base_32_1[i] = RandomBytes(32);
base_32_2[i] = RandomBytes(32);
base_20_1[i] = RandomBytes(20);
base_20_2[i] = RandomBytes(20);
}
}

private byte[] RandomBytes(int count)
{
byte[] randomBytes = new byte[count];
random.NextBytes(randomBytes);
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()
{
// testing "official version"
UInt256[] uut_32_1 = new UInt256[MAX_TESTS];
UInt256[] uut_32_2 = new UInt256[MAX_TESTS];

for(var i=0; i<MAX_TESTS; i++)
{
uut_32_1[i] = new UInt256(base_32_1[i]);
uut_32_2[i] = new UInt256(base_32_2[i]);
}

var checksum0 = Benchmark(() =>
jsolman marked this conversation as resolved.
Show resolved Hide resolved
{
var checksum = 0;
for(var i=0; i<MAX_TESTS; i++)
{
checksum += uut_32_1[i].CompareTo(uut_32_2[i]);
}

return checksum;
}).Item2;

var checksum1 = Benchmark(() =>
{
var checksum = 0;
for(var i=0; i<MAX_TESTS; i++)
{
checksum += code1_UInt256CompareTo(base_32_1[i], base_32_2[i]);
}

return checksum;
}).Item2;

var checksum2 = Benchmark(() =>
{
var checksum = 0;
for(var i=0; i<MAX_TESTS; i++)
{
checksum += code2_UInt256CompareTo(base_32_1[i], base_32_2[i]);
}

return checksum;
}).Item2;

var checksum3 = Benchmark(() =>
{
var checksum = 0;
for(var i=0; i<MAX_TESTS; i++)
{
checksum += code3_UInt256CompareTo(base_32_1[i], base_32_2[i]);
}

return checksum;
}).Item2;

checksum0.Should().Be(checksum1);
checksum0.Should().Be(checksum2);
checksum0.Should().Be(0);
}

private int code1_UInt256CompareTo(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_UInt256CompareTo(byte[] b1, byte[] b2)
{
fixed (byte* px = b1, py = b2)
{
uint* lpx = (uint*)px;
uint* lpy = (uint*)py;
for (int i = 7; 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 = 3; i >= 0; i--)
{
if (lpx[i] > lpy[i])
return 1;
if (lpx[i] < lpy[i])
return -1;
}
}
return 0;
}

}
}
1 change: 1 addition & 0 deletions neo.UnitTests/neo.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Neo.UnitTests</AssemblyName>
<RootNamespace>Neo.UnitTests</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down