Created
November 25, 2021 09:33
-
-
Save teodutu/c6b01561e8eb0d12f9fc7ce11b519d9a to your computer and use it in GitHub Desktop.
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
module benchmark_d_arrayctor; | |
import std.stdio : writeln; | |
import std.datetime.stopwatch : benchmark; | |
import std.math : sqrt; | |
import std.algorithm : reduce; | |
template GenStruct(string Size, string Var, string Buf) | |
{ | |
const char[] GenStruct = "struct _" ~ Size ~ "B_Struct { " ~ Buf ~ " this(this) { } }\n" ~ | |
"_" ~ Size ~ "B_Struct[1] " ~ Var ~ "s;\n" ~ | |
"_" ~ Size ~ "B_Struct[64] " ~ Var ~ "m;\n" ~ | |
"_" ~ Size ~ "B_Struct[256] " ~ Var ~ "l;\n\n"; | |
} | |
mixin(GenStruct!("0", "e", "")); | |
mixin(GenStruct!("64", "m", "char[64] x;")); | |
mixin(GenStruct!("256", "l", "char[256] x;")); | |
template GenTest(string Struct, string Size, string arr) | |
{ | |
const char[] GenTest = "void test" ~ Struct ~ Size ~ "() { " ~ Struct ~ "[" ~ arr ~ ".length] a = " ~ arr ~ "; }"; | |
} | |
mixin(GenTest!("_0B_Struct", "_1Elem", "es")); | |
mixin(GenTest!("_0B_Struct", "_64Elems", "em")); | |
mixin(GenTest!("_0B_Struct", "_256Elems", "el")); | |
mixin(GenTest!("_64B_Struct", "_1Elem", "ms")); | |
mixin(GenTest!("_64B_Struct", "_64Elems", "mm")); | |
mixin(GenTest!("_64B_Struct", "_256Elems", "ml")); | |
mixin(GenTest!("_256B_Struct", "_1Elem", "ls")); | |
mixin(GenTest!("_256B_Struct", "_64Elems", "lm")); | |
mixin(GenTest!("_256B_Struct", "_256Elems", "ll")); | |
void runTest(void function() func, string funcName, uint runs) | |
{ | |
double[] times; | |
for (int j = 0; j != 100; ++j) | |
times ~= cast(double) benchmark!(func)(runs)[0].total!"msecs"; | |
auto n = times.length; | |
auto avg = reduce!((a, b) => a + b / n)(0.0f, times); | |
auto sd = sqrt(reduce!((a, b) => a + (b - avg) * (b - avg) / n)(0.0f, times)); | |
writeln(funcName, " @ ", runs, " runs:\taverage time = ", avg, "ms; \tstd dev = ", sd); | |
} | |
void main() | |
{ | |
enum sizes = ["_1Elem", "_64Elems", "_256Elems"]; | |
enum structs = ["_0B_Struct", "_64B_Struct", "_256B_Struct"]; | |
static foreach (st; structs) | |
static foreach (i, sz; sizes) | |
mixin("runTest(&test" ~ st ~ sz ~ ", \"" ~ st ~ sz ~ "\", 1_000_000U);"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment