Skip to content

Instantly share code, notes, and snippets.

@MartinNowak
Last active August 29, 2015 14:08
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 MartinNowak/18da24cf205d9064e3ff to your computer and use it in GitHub Desktop.
Save MartinNowak/18da24cf205d9064e3ff to your computer and use it in GitHub Desktop.
bench hash table
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