| 1 |
private import tango.util.collection.HashMap; |
|---|
| 2 |
private import tango.util.time.StopWatch; |
|---|
| 3 |
private import tango.math.Random; |
|---|
| 4 |
private import tango.io.Stdout; |
|---|
| 5 |
|
|---|
| 6 |
char[] randomString(uint length) |
|---|
| 7 |
{ |
|---|
| 8 |
char[] rtn = new char[length]; |
|---|
| 9 |
static char[] randomChars = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|---|
| 10 |
for (uint i = 0; i < length; i++) |
|---|
| 11 |
rtn[i] = randomChars[tango.math.Random.Random.shared.next(randomChars.length)]; |
|---|
| 12 |
return rtn; |
|---|
| 13 |
} |
|---|
| 14 |
|
|---|
| 15 |
private class TestClass |
|---|
| 16 |
{ |
|---|
| 17 |
int _number; |
|---|
| 18 |
char[] _string; |
|---|
| 19 |
|
|---|
| 20 |
int number() { return _number; } |
|---|
| 21 |
void number(int number) { return _number = number; } |
|---|
| 22 |
char[] string() { return _string; } |
|---|
| 23 |
void string(char[] string) { return _string = string; } |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
void main() |
|---|
| 27 |
{ |
|---|
| 28 |
int keySize = 10; |
|---|
| 29 |
int iterations = 2500000; |
|---|
| 30 |
|
|---|
| 31 |
char[][] ciKeys; |
|---|
| 32 |
for (int i = 0; i < iterations; i++) |
|---|
| 33 |
ciKeys ~= randomString(keySize); |
|---|
| 34 |
StopWatch charKeyTimer; |
|---|
| 35 |
auto ciHashMap = new HashMap!(char[], int); |
|---|
| 36 |
charKeyTimer.start(); |
|---|
| 37 |
for (int i = 0; i < iterations; i++) |
|---|
| 38 |
ciHashMap[ciKeys[i]] = i; |
|---|
| 39 |
Stdout.format("charKey put time: {}", cast(double)charKeyTimer.stop()).newline; |
|---|
| 40 |
charKeyTimer.start(); |
|---|
| 41 |
foreach(char[] key; ciKeys) |
|---|
| 42 |
ciHashMap[key]; |
|---|
| 43 |
Stdout.format("charKey get time: {}", cast(double)charKeyTimer.stop()).newline; |
|---|
| 44 |
|
|---|
| 45 |
TestClass[] oiKeys; |
|---|
| 46 |
for (int i = 0; i < iterations; i++) |
|---|
| 47 |
oiKeys ~= new TestClass; |
|---|
| 48 |
StopWatch classKeyTimer; |
|---|
| 49 |
auto oiHashMap = new HashMap!(TestClass, int); |
|---|
| 50 |
classKeyTimer.start(); |
|---|
| 51 |
for (int i = 0; i < iterations; i++) |
|---|
| 52 |
oiHashMap[oiKeys[i]] = i; |
|---|
| 53 |
Stdout.format("classKey put time: {}", cast(double)classKeyTimer.stop()).newline; |
|---|
| 54 |
classKeyTimer.start(); |
|---|
| 55 |
foreach(TestClass key; oiKeys) |
|---|
| 56 |
oiHashMap[key]; |
|---|
| 57 |
Stdout.format("classKey get time: {}", cast(double)classKeyTimer.stop()).newline; |
|---|
| 58 |
|
|---|
| 59 |
// null key != empty string key |
|---|
| 60 |
/*auto ccHashMap = new HashMap!(char[], char[]); |
|---|
| 61 |
ccHashMap[null] = "null"; |
|---|
| 62 |
ccHashMap[""] = "empty string"; |
|---|
| 63 |
char[] ccOutNull = ccHashMap[null]; |
|---|
| 64 |
char[] ccOutEmpty = ccHashMap[""]; |
|---|
| 65 |
Stdout.format("ccOutNull: '{}' (null: {}), ccOutEmpty: '{}' (null: {})", ccOutNull, ccOutNull is null, ccOutEmpty, ccOutEmpty is null).newline;*/ |
|---|
| 66 |
|
|---|
| 67 |
// classed null keys allowed |
|---|
| 68 |
/*TestClass ocNullClass = null; |
|---|
| 69 |
TestClass ocClass = new TestClass; |
|---|
| 70 |
ocClass.number = 16; |
|---|
| 71 |
ocClass.string = "myString"; |
|---|
| 72 |
auto ocHashMap = new HashMap!(TestClass, char[]); |
|---|
| 73 |
ocHashMap[ocNullClass] = "null class"; |
|---|
| 74 |
ocHashMap[ocClass] = "new class"; |
|---|
| 75 |
char[] ocOutNull = ocHashMap[null]; |
|---|
| 76 |
char[] ocOutNew = ocHashMap[ocClass]; |
|---|
| 77 |
Stdout.format("ocOutNull: '{}' (null: {}), ocOutNew: '{}' (null: {})", ocOutNull, ocOutNull is null, ocOutNew, ocOutNew is null).newline;*/ |
|---|
| 78 |
|
|---|
| 79 |
// null elements allowed |
|---|
| 80 |
/*TestClass coNullClass = null; |
|---|
| 81 |
TestClass coClass = new TestClass; |
|---|
| 82 |
coClass.number = 16; |
|---|
| 83 |
coClass.string = "myString"; |
|---|
| 84 |
auto coHashMap = new HashMap!(char[], TestClass); |
|---|
| 85 |
coHashMap[null] = null; |
|---|
| 86 |
coHashMap["nullClass"] = coNullClass; |
|---|
| 87 |
coHashMap["myClass"] = coClass; |
|---|
| 88 |
TestClass coOutNull = coHashMap[null]; |
|---|
| 89 |
TestClass coOutNullClass = coHashMap["nullClass"]; |
|---|
| 90 |
TestClass coOutClass = coHashMap["myClass"]; |
|---|
| 91 |
Stdout.format("coOutNull: ({}), coOutNullClass: ({}), coOutClass.number: {}", coOutNull is null, coOutNullClass is null, coOutClass.number).newline;*/ |
|---|
| 92 |
} |
|---|