FastRng/FastRngTests/PerformanceTests.cs

154 lines
5.6 KiB
C#
Raw Permalink Normal View History

2020-11-01 13:04:57 +00:00
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
2023-07-06 08:08:46 +00:00
using FastRng;
using FastRng.Distributions;
2020-11-01 13:04:57 +00:00
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;
using NUnit.Framework;
namespace FastRngTests;
[ExcludeFromCodeCoverage]
public class PerformanceTests
2020-11-01 13:04:57 +00:00
{
#region FastRng
2020-11-01 13:04:57 +00:00
[Test]
[Category(TestCategories.PERFORMANCE)]
public async Task Generate1MUniformMultiThreaded()
{
using var rng = new MultiThreadedRng<float>();
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
2020-11-01 13:04:57 +00:00
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = rng.GetUniform();
stopwatch.Stop();
TestContext.WriteLine($"Generated 1M uniform distributed random numbers in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
}
[Test]
[Category(TestCategories.PERFORMANCE)]
public async Task Generate1MUniformMultiChannel()
{
using var rng = new MultiChannelRng<float>();
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = rng.GetUniform();
2020-11-01 13:04:57 +00:00
stopwatch.Stop();
2020-11-07 11:41:44 +00:00
TestContext.WriteLine($"Generated 1M uniform distributed random numbers in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
}
2020-11-01 13:04:57 +00:00
[Test]
[Category(TestCategories.PERFORMANCE)]
public async Task Generate1MNormal()
{
using var rng = new MultiThreadedRng<float>();
var dist = new NormalS02M05<float>(rng);
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = dist.NextNumber();
2020-11-01 13:04:57 +00:00
stopwatch.Stop();
TestContext.WriteLine($"Generated 1M normal distributed random numbers in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
}
2020-11-01 13:04:57 +00:00
[Test]
[Category(TestCategories.PERFORMANCE)]
public async Task Generate1MChiSquare()
{
using var rng = new MultiThreadedRng<float>();
var dist = new ChiSquareK4<float>(rng);
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
2020-11-01 13:04:57 +00:00
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = dist.NextNumber();
2020-11-01 13:04:57 +00:00
stopwatch.Stop();
2020-11-07 11:41:44 +00:00
TestContext.WriteLine($"Generated 1M chi-square distributed random numbers in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
}
2020-11-01 13:04:57 +00:00
#endregion
2020-11-01 13:04:57 +00:00
#region Math.NET
2020-11-01 13:04:57 +00:00
[Test]
[Category(TestCategories.PERFORMANCE)]
public void ComparisonMathNet1MUniform()
{
var rng = new Xorshift(true);
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
2020-11-01 13:04:57 +00:00
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = (float) rng.NextDouble();
2020-11-01 13:04:57 +00:00
stopwatch.Stop();
2020-11-01 13:04:57 +00:00
TestContext.WriteLine($"Generated 1M uniform distributed random numbers by means of Math.NET in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
}
2020-11-01 13:04:57 +00:00
[Test]
[Category(TestCategories.PERFORMANCE)]
public void ComparisonMathNet1MNormal()
{
var rng = new Xorshift(true);
var dist = new Normal(stddev: 0.2f, mean: 0.5f, randomSource: rng);
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
2020-11-01 13:04:57 +00:00
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = (float) dist.Sample();
2020-11-01 13:04:57 +00:00
stopwatch.Stop();
2020-11-01 13:04:57 +00:00
TestContext.WriteLine($"Generated 1M normal distributed random numbers by means of Math.NET in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
}
2020-11-01 13:04:57 +00:00
[Test]
[Category(TestCategories.PERFORMANCE)]
public void ComparisonMathNet1MChiSquare()
{
var rng = new Xorshift(true);
var dist = new ChiSquared(4);
var data = new float[1_000_000];
var stopwatch = new Stopwatch();
Thread.Sleep(TimeSpan.FromSeconds(10)); // Warm-up phase of generator
2020-11-01 13:04:57 +00:00
stopwatch.Start();
for (uint n = 0; n < data.Length; n++)
data[n] = (float) dist.Sample();
2020-11-01 13:04:57 +00:00
stopwatch.Stop();
2020-11-01 13:04:57 +00:00
TestContext.WriteLine($"Generated 1M chi-squared distributed random numbers by means of Math.NET in {stopwatch.Elapsed.Minutes} minute(s), {stopwatch.Elapsed.Seconds} second(s), and {stopwatch.Elapsed.Milliseconds} milliseconds.");
2020-11-01 13:04:57 +00:00
}
#endregion
2020-11-01 13:04:57 +00:00
}