Skip to content

Commit

Permalink
优化内存 (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Sep 19, 2024
2 parents 3127594 + 8105b00 commit 628609c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/EasilyNET.Core/Misc/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public static class RandomExtensions
// ReSharper disable once UnusedParameter.Global
public static int StrictNext(this Random rand, int startIndex = 0, int maxValue = int.MaxValue)
{
var buffer = new byte[4];
using (var rng = RandomNumberGenerator.Create())
if (startIndex > maxValue)
{
rng.GetBytes(buffer);
throw new ArgumentOutOfRangeException(nameof(startIndex), "startIndex must be less than maxValue.");
}
var randomValue = BitConverter.ToInt32(buffer, 0);
Span<byte> buffer = stackalloc byte[4];
RandomNumberGenerator.Fill(buffer);
var randomValue = BitConverter.ToInt32(buffer);
return new Random(randomValue).Next(startIndex, maxValue);
}

Expand All @@ -48,4 +49,4 @@ public static double NextGauss(this Random rand, double mean, double stdDev)
var randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
return mean + stdDev * randStdNormal;
}
}
}
30 changes: 16 additions & 14 deletions test/EasilyNET.Core.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using EasilyNET.Core.Misc;

namespace EasilyNET.Core.Benchmark;

// ReSharper disable ClassNeverInstantiated.Global
/// <summary>
/// </summary>
[Config(typeof(Config))]
public class AsyncSemaphoreBenchmark
public class AsyncStrictNextBenchmark
{
// private readonly AsyncSemaphore _semaphore = new(1);
//
// private readonly AsyncSemaphoreCas _semaphoreCas = new();
//

private readonly Random _random = new Random();

// [Benchmark]
// public async Task Asynchronously()
// public void TestStrictNext()
// {
// var task = _semaphore.WaitAsync();
// _semaphore.Release();
// await task;
// for (int i = 0; i < 5000; i++)
// {
// _random.StrictNext();
// }
// }
//
// [Benchmark]
// public async Task AsynchronouslyCAS()
// public void TestStrictNext2()
// {
// var task = _semaphore.WaitAsync();
// _semaphore.Release();
// await task;
// for (int i = 0; i < 5000; i++)
// {
// _random.StrictNext2();
// }
// }

private class Config : ManualConfig
Expand All @@ -50,6 +52,6 @@ public static class Program
/// </summary>
public static void Main()
{
_ = BenchmarkRunner.Run<AsyncSemaphoreBenchmark>();
_ = BenchmarkRunner.Run<AsyncStrictNextBenchmark>();
}
}
26 changes: 26 additions & 0 deletions test/EasilyNET.Test.Unit/Randoms/RandomTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using EasilyNET.Core.Misc;

namespace EasilyNET.Test.Unit.Randoms;
[TestClass]
public class RandomTest
{
private readonly Random _random = new Random();

[TestMethod]
public void StrictNext_ShouldReturnValueWithinRange()
{
// Act
int result = _random.StrictNext();

// Assert
Assert.IsTrue(result is >= 0 and < int.MaxValue);
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void StrictNext2_ShouldThrowArgumentOutOfRangeException_WhenStartIndexIsGreaterThanOrEqualToMaxValue()
{
// Act
_random.StrictNext(10,5);
}
}

0 comments on commit 628609c

Please sign in to comment.