Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2011 21:20
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/893340 to your computer and use it in GitHub Desktop.
Save anonymous/893340 to your computer and use it in GitHub Desktop.
module perfTest;
import std.algorithm;
import std.stdio;
import std.random;
import std.datetime;
int[string] Aa;
string[] Arr;
string findMe;
static this()
{
foreach (i; 0 .. 128)
{
string str;
str ~= (cast(char)uniform(97, 123));
str ~= (cast(char)uniform(97, 123));
str ~= (cast(char)uniform(97, 123));
str ~= (cast(char)uniform(97, 123));
str ~= (cast(char)uniform(97, 123));
if (i == 64)
{
findMe = str;
}
Arr ~= str;
Aa[str] = 0;
}
assert(Arr.length == Aa.length); // just in case Aa is smaller due to duplicates
}
void AAForeach()
{
foreach (value; Aa)
{
}
}
void ArrForeach()
{
foreach (value; Arr)
{
}
}
auto AAFind()
{
return (findMe in Aa);
}
auto ArrFind()
{
return canFind(Arr, findMe);
}
void main()
{
writeln();
auto a = benchmark!(AAForeach)(1000);
writefln("AA Foreach: %s", a[0].to!("usecs", int));
auto b = benchmark!(ArrForeach)(1000);
writefln("Arr Foreach: %s", b[0].to!("usecs", int));
writeln();
auto c = benchmark!(AAFind)(10000);
writefln("AA Lookup: %s", c[0].to!("usecs", int));
auto d = benchmark!(ArrFind)(10000);
writefln("Arr Lookup: %s", d[0].to!("usecs", int));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment