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
Changes from 1 commit
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
98 changes: 57 additions & 41 deletions neo.UnitTests/UT_UIntBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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<MAX_TESTS; i++)
{
checksum0 += uut_32_1[i].CompareTo(uut_32_2[i]);
}
sw0.Stop();
TimeSpan time0 = sw0.Elapsed;
Console.WriteLine("Elapsed={0} Sum={1}",time0, checksum0);

// testing code1 algorithm
Stopwatch sw1 = new Stopwatch();
sw1.Start();
int checksum1 = 0;
for(var i=0; i<MAX_TESTS; i++)
var checksum0 = Benchmark(() =>
jsolman marked this conversation as resolved.
Show resolved Hide resolved
{
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<MAX_TESTS; i++)
{
checksum += uut_32_1[i].CompareTo(uut_32_2[i]);
}

return checksum;
}).Item2;

// testing code2 algorithm
Stopwatch sw2 = new Stopwatch();
sw2.Start();
int checksum2 = 0;
for(var i=0; i<MAX_TESTS; i++)
var checksum1 = Benchmark(() =>
{
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<MAX_TESTS; i++)
{
checksum += code1_UInt256CompareTo(base_32_1[i], base_32_2[i]);
}

Console.WriteLine("Elapsed={0} Sum={1}",time2, checksum2);
return checksum;
}).Item2;

// testing code3 algorithm
Stopwatch sw3 = new Stopwatch();
sw3.Start();
int checksum3 = 0;
for(var i=0; i<MAX_TESTS; i++)
var checksum2 = Benchmark(() =>
{
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<MAX_TESTS; i++)
{
checksum += code2_UInt256CompareTo(base_32_1[i], base_32_2[i]);
}

Console.WriteLine("Elapsed={0} Sum={1}",time3, checksum3);
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(checksum3);
checksum0.Should().Be(0);
}

private int code1_UInt256CompareTo(byte[] b1, byte[] b2)
Expand Down