1 // Run via: dub -c benchmark
2 
3 /*
4 Last run:
5 
6 Simple: 527 ms, 935 μs, and 3 hnsecs (average: ~52 μs and 7 hnsecs)
7 Complex: 1 sec, 233 ms, 402 μs, and 1 hnsec (average: ~123 μs and 3 hnsecs)
8 Exploding: 695 ms, 192 μs, and 3 hnsecs (average: ~69 μs and 5 hnsecs)
9 Grouped: 1 sec, 516 ms, 823 μs, and 2 hnsecs (average: ~151 μs and 6 hnsecs)
10 */
11 
12 module examples.benchmark;
13 
14 import std.datetime.stopwatch, std.stdio;
15 import dice;
16 
17 private static simpleInput = "3d6+6";
18 private static complexInput = "20d6+1d8+30d6k17*1337";
19 private static explodingInput = "20d6!+20d4!";
20 private static groupedInput = "{2d6!,3d6!,4d6!,5d6!}k2";
21 
22 ///
23 float rollSimple() {
24   return roll(simpleInput);
25 }
26 
27 ///
28 float rollComplex() {
29   return roll(complexInput);
30 }
31 
32 ///
33 float rollExploding() {
34   return roll(explodingInput);
35 }
36 
37 ///
38 float rollGrouped() {
39   return roll(groupedInput);
40 }
41 
42 void main() {
43   auto times = 10_000;
44   auto results = benchmark!(rollSimple, rollComplex, rollExploding, rollGrouped)(times);
45   writefln("Simple: %s (average: ~%s)", results[0], results[0] / times);
46   writefln("Complex: %s (average: ~%s)", results[1], results[1] / times);
47   writefln("Exploding: %s (average: ~%s)", results[2], results[2] / times);
48   writefln("Grouped: %s (average: ~%s)", results[3], results[3] / times);
49 }