Last active
August 29, 2015 14:08
-
-
Save MartinNowak/18da24cf205d9064e3ff to your computer and use it in GitHub Desktop.
bench hash table
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 std.file, std.algorithm, std.stdio, std.random, std.math; | |
extern (C++) | |
{ | |
struct StringValue | |
{ | |
void *ptrvalue; | |
size_t length; | |
char[0] lstring; | |
} | |
void tab_init(); | |
StringValue *tab_update(const char * key, size_t len); | |
void tab_report(); | |
} | |
// exponential distribution around mean | |
struct ExpRandom | |
{ | |
double mean; | |
Random gen; | |
this(double mean) | |
{ | |
this.mean = mean; | |
gen = Random(unpredictableSeed); | |
} | |
size_t front() | |
{ | |
return lround(mean * -log(uniform!"()"(0.0, 1.0, gen))); | |
} | |
alias gen this; | |
} | |
struct CacheStomper | |
{ | |
ExpRandom rnd; | |
size_t i; | |
ubyte[] mem; | |
this(size_t avgBytesPerCall) | |
{ | |
rnd = ExpRandom(avgBytesPerCall / 64.0); | |
mem = new ubyte[](32 * 1024 * 1024); | |
} | |
void stomp() | |
{ | |
immutable n = rnd.front(); rnd.popFront(); | |
foreach (_; 0 .. n) | |
++mem[(i += 64) & ($ - 1)]; | |
} | |
} | |
void main() | |
{ | |
immutable text = cast(string)read("dante.txt"); | |
tab_init(); | |
auto stomper = CacheStomper(1000); | |
foreach (_; 0 .. 20) | |
{ | |
foreach (word; text.splitter(' ')) | |
{ | |
auto sv = tab_update(word.ptr, word.length); | |
++sv.ptrvalue; | |
stomper.stomp(); | |
} | |
} | |
//tab_report(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment