source: misc/tools/HeuristicLab.Benchmarks/HeuristicLab.Benchmarks/Random/RandomPerformance.cs @ 16299

Last change on this file since 16299 was 16299, checked in by bburlacu, 9 months ago

#2963: Add first version of HeuristicLab.Benchmarks

File size: 1.4 KB
Line 
1using BenchmarkDotNet.Attributes;
2using HeuristicLab.Core;
3using HeuristicLab.Random;
4
5namespace HeuristicLab.Benchmarks {
6  [CoreJob, ClrJob, MonoJob]
7  [MinColumn, MaxColumn, MeanColumn, MedianColumn]
8  public class RandomPerformance {
9    //private readonly IRandom jsfRandom = new JsfRandom(1234);
10    private readonly IRandom mersenneTwister = new MersenneTwister(1234);
11    private readonly IRandom fastRandom = new FastRandom(1234);
12
13    [Params(1_000_000)]
14    public int N { get; set; }
15
16    //[Benchmark] public double SimpleRandomInt() => IntPerformance(jsfRandom);
17    [Benchmark] public double FastRandomInt() => IntPerformance(fastRandom);
18    [Benchmark] public double MersenneTwisterInt() => IntPerformance(mersenneTwister);
19
20    //[Benchmark] public double SimpleRandomDouble() => DoublePerformance(jsfRandom);
21    [Benchmark] public double FastRandomDouble() => DoublePerformance(fastRandom);
22    [Benchmark] public double MersenneTwisterDouble() => DoublePerformance(mersenneTwister);
23
24    #region helper methods
25    private double IntPerformance(IRandom random) {
26      double avg = 0;
27      for (int i = 0; i < N; ++i) {
28        avg += random.Next();
29      }
30      return avg;
31    }
32
33    private double DoublePerformance(IRandom random) {
34      double avg = 0;
35      for (int i = 0; i < N; ++i) {
36        avg += random.NextDouble();
37      }
38      return avg;
39    }
40    #endregion
41  }
42}
Note: See TracBrowser for help on using the repository browser.