Created
March 29, 2011 21:20
-
-
Save anonymous/893340 to your computer and use it in GitHub Desktop.
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
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