Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark dstruct hasmaps against associative arrays.
import dstruct.map;
enum maxSize = 1000;
struct AddAndRemove {
static void func(T)() {
T map;
foreach(x; 0 .. maxSize) {
map[x] = x;
}
foreach(x; 0 .. maxSize) {
map.remove(x);
}
}
}
struct AddAndFetch {
static void func(T)() {
T map;
foreach(x; 0 .. maxSize) {
map[x] = x;
}
foreach(x; 0 .. maxSize) {
auto y = map[x];
}
}
}
struct AddAndGet {
static void func(T)() {
T map;
foreach(x; 0 .. maxSize) {
map[x] = x;
}
foreach(x; 0 .. maxSize) {
auto y = map.get(x, 0);
}
}
}
struct AddAndIn {
static void func(T)() {
T map;
foreach(x; 0 .. maxSize) {
map[x] = x;
}
foreach(x; 0 .. maxSize) {
auto y = x in map;
}
}
}
void benchmark(Wrapper)() {
import std.stdio;
import std.datetime;
auto result = comparingBenchmark!(
Wrapper.func!(int[int]),
Wrapper.func!(HashMap!(int, int)),
1000
);
writefln("%s: %dms -> %dms",
Wrapper.stringof,
result.baseTime.msecs,
result.targetTime.msecs
);
}
void main(string[] argv) {
import std.stdio;
benchmark!AddAndRemove;
benchmark!AddAndFetch;
benchmark!AddAndGet;
benchmark!AddAndIn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment