Created
May 25, 2014 14:14
-
-
Save anonymous/4a060b8f65684eb0586c to your computer and use it in GitHub Desktop.
Benchmark dstruct hasmaps against associative arrays.
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
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