Skip to content

Instantly share code, notes, and snippets.

@Zshazz

Zshazz/diceTest.d

Created Dec 1, 2013
Embed
What would you like to do?
Test code & results for std.random.dice that accepts SortedRange
import std.stdio, std.datetime, std.random, std.range, std.typetuple;
Random rng;
void main() {
rng = Random(123102);
alias propSizes = TypeTuple!(5, 10, 500, 100_000, 10_000_000);
foreach(i, propSize; propSizes) {
foreach(j, fn; TypeTuple!(testNormal, testSorted)) {
static if(j == 0) {
int numRuns = 50_000_000 / propSize;
} else {
int numRuns = 5_000_000;
}
alias Func = testDiceAt!(fn, propSize);
auto bench = benchmark!Func(numRuns)[0].msecs / (1.0 * numRuns);
writeln("testDiceAt!(", __traits(identifier, fn), ", ", propSize, ")() average run: ", bench, "ms");
}
}
}
size_t testNormal(size_t size) {
Random reproRng = rng.save;
return dice(reproRng, iota(size));
}
size_t testSorted(size_t size) {
Random reproRng = rng.save;
return dice(reproRng, iota(size).assumeSorted);
}
void testDiceAt(alias fn, size_t PropSize)() {
if(fn(PropSize) == -2)
assert(0);
}
testDiceAt!(testNormal, 5)() average run: 0.0003056ms
testDiceAt!(testSorted, 5)() average run: 0.0002572ms
testDiceAt!(testNormal, 10)() average run: 0.0004854ms
testDiceAt!(testSorted, 10)() average run: 0.0002894ms
testDiceAt!(testNormal, 500)() average run: 0.0167ms
testDiceAt!(testSorted, 500)() average run: 0.0003668ms
testDiceAt!(testNormal, 100000)() average run: 3.29ms
testDiceAt!(testSorted, 100000)() average run: 0.0005278ms
testDiceAt!(testNormal, 10000000)() average run: 325.8ms
testDiceAt!(testSorted, 10000000)() average run: 0.0006486ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment