forked from bchavez/Bogus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Benchmark project to latest version of BenchmarkDotNet.
- Loading branch information
Showing
10 changed files
with
142 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,3 +171,4 @@ $RECYCLE.BIN/ | |
.DS_Store | ||
/nuget.push.bat | ||
|
||
/Source/Benchmark/BenchmarkDotNet.Artifacts/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Threading; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
using Bogus; | ||
|
||
namespace Benchmark | ||
{ | ||
[SimpleJob(RuntimeMoniker.NetCoreApp31), SimpleJob(RuntimeMoniker.Net471)] | ||
[MarkdownExporter, MemoryDiagnoser, RPlotExporter] | ||
public class BenchRandomNumber | ||
{ | ||
private NumberTests n; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
n = new NumberTests(); | ||
} | ||
|
||
[Benchmark] | ||
public int OldMethod() | ||
{ | ||
return n.Number(int.MinValue, int.MaxValue); | ||
} | ||
|
||
[Benchmark] | ||
public int BitShift() | ||
{ | ||
return n.NumberBitShift(int.MinValue, int.MaxValue); | ||
} | ||
|
||
[Benchmark] | ||
public int JDGMethod() | ||
{ | ||
return n.NumberJDG(int.MinValue, int.MaxValue); | ||
} | ||
} | ||
|
||
public class NumberTests | ||
{ | ||
internal static Lazy<object> Locker = new Lazy<object>(() => new object(), LazyThreadSafetyMode.ExecutionAndPublication); | ||
|
||
private readonly Random localSeed = new System.Random(); | ||
|
||
private static byte[] temp = new byte[4]; | ||
|
||
public int Number(int min = 0, int max = 1) | ||
{ | ||
//lock any seed access, for thread safety. | ||
lock (Locker.Value) | ||
{ | ||
//Clamp max value, Issue #30. | ||
max = max == int.MaxValue ? max : max + 1; | ||
return localSeed.Next(min, max); | ||
} | ||
} | ||
|
||
public int NumberBitShift(int min = 0, int max = 1) | ||
{ | ||
//lock any seed access, for thread safety. | ||
lock (Locker.Value) | ||
{ | ||
// Adjust the range as needed to make max inclusive. The Random.Next function uses exclusive upper bounds. | ||
|
||
// If max can be extended by 1, just do that. | ||
if (max < int.MaxValue) return localSeed.Next(min, max + 1); | ||
|
||
// If max is exactly int.MaxValue, then check if min can be used to push the range out by one the other way. | ||
// If so, then we can simply add one to the result to put it back in the correct range. | ||
if (min > int.MinValue) return 1 + localSeed.Next(min - 1, max); | ||
|
||
localSeed.NextBytes(temp); | ||
return temp[0] << 24 | temp[1] << 16 | temp[2] << 8 | temp[3]; | ||
} | ||
} | ||
|
||
public int NumberJDG(int min = 0, int max = 1) | ||
{ | ||
//lock any seed access, for thread safety. | ||
lock (Locker.Value) | ||
{ | ||
// Adjust the range as needed to make max inclusive. The Random.Next function uses exclusive upper bounds. | ||
|
||
// If max can be extended by 1, just do that. | ||
if (max < int.MaxValue) return localSeed.Next(min, max + 1); | ||
|
||
// If max is exactly int.MaxValue, then check if min can be used to push the range out by one the other way. | ||
// If so, then we can simply add one to the result to put it back in the correct range. | ||
if (min > int.MinValue) return 1 + localSeed.Next(min - 1, max); | ||
|
||
// If we hit this line, then min is int.MinValue and max is int.MaxValue, which mean the caller wants a | ||
// number from a range spanning all possible values of int. The Random class only supports exclusive | ||
// upper bounds, period, and the upper bound must be specified as an int, so the best we can get in a | ||
// single call is a value in the range (int.MinValue, int.MaxValue - 1). Instead, what we do is get two | ||
// samples, one in the range (int.MinValue, -1) and the other as unbiased as possible, and using the | ||
// second one to decide, 50% of the time we invert all the bits in the sample, shifting its range to | ||
// (0, int.MaxValue). | ||
var result = localSeed.Next(int.MinValue, 0); | ||
|
||
if ((localSeed.Next() & 0x10000000) == 0) | ||
result = ~result; | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Attributes.Exporters; | ||
using Bogus; | ||
|
||
namespace Benchmark | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
### Running Benchmarks | ||
|
||
When running benchmarks, please make sure you compile this project in `Release` mode. | ||
**Requirements:** | ||
* **.NET 3.1 SDK** or later. | ||
* **[`R` project for Statistical Computing](https://www.r-project.org/) version 3.3.3 (2017-03-06)** or later. | ||
* Ensure `rscript.exe` is in your path as [instructed here](https://benchmarkdotnet.org/articles/configs/exporters.html#plots) so that plots can be generated. | ||
|
||
Once compiled go to `\bin\Release\` and execute from the command line: | ||
You must compile this `Benchmark` project in `Release` mode. | ||
|
||
```csharp | ||
dotnet build -c Release | ||
``` | ||
|
||
After compiling in `Release` mode, execute the following command from the command line: | ||
``` | ||
$> Benchmark.exe | ||
$> dotnet benchmark bin\Release\netstandard2.0\Benchmark.dll | ||
``` | ||
|
||
Your benchmark results will be in `\bin\Release\BenchmarkDotNet.Artifacts`. | ||
Pick your benchmark to run. Your benchmark results will be in `\BenchmarkDotNet.Artifacts`. | ||
|
||
**DO NOT** attempt to run these benchmarks through **Visual Studio**'s debugger or **F5** runner. | ||
### Other Notes | ||
|
||
Also, make sure you've installed [**`R`**](https://www.r-project.org/) and that `rscript.exe` is in your path as [instructed here](http://benchmarkdotnet.org/Configs/Exporters.htm#plots) so that plots can be generated. | ||
**DO NOT** attempt to run these benchmarks through **Visual Studio**'s debugger or **F5** runner. | ||
|
||
More on benchmarking: | ||
http://benchmarkdotnet.org/index.htm |