Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created May 25, 2014 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4a060b8f65684eb0586c to your computer and use it in GitHub Desktop.
Save anonymous/4a060b8f65684eb0586c to your computer and use it in GitHub Desktop.
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