Honey, I sped up the associated array

Lionello Lunesu lio at lunesu.remove.com
Fri Oct 13 07:15:23 PDT 2006


Dave wrote:
> Lionello Lunesu wrote:
>> Some claim, so numbers first:
>>
>> As a benchmark, I've used my implementation of the program mentioned 
>> in the thread "Lisp vs. C++ (not off-topic)", in a critical-priority 
>> thread. Results are averaged over 8 runs. System: AMD X2 4600+, 
>> Windows Vista RC1 x64, 1GB.
>>
>> Old AA: 563ms
>> New AA: 445ms
>>
> 
> There were 4 Shootout Benchmark tests using hash tables - one is still 
> active:
> 
> http://shootout.alioth.debian.org/gp4/benchmark.php?test=knucleotide&lang=dlang&id=2 
> 
> 
> Old AA: 1.85
> New AA: 1.85
> 
> For the three older tests:
> 
> hash Old: 2.55
>      New: 3.02
> 
> hash2 Old: 3.36
>       New: 2.88
> 
> wordfreq Old: 0.178
>          New: 0.170
> 
> (These were run as they are/were on the Shootout)
 >
> So it's kind-of 6 of one and 1/2 dozen of another for those tests, but 
> your new version sure seems
> more elegant!

I also like powers-of-2 more than primes ;)

> Based on this and looking at the code for hash.d where the new version 
> doesn't perform as well, it looks like the new version has more 
> collisions.. If the getHash() in std/typeinfo/ti_Aa.d could be changed 
> to provide a more unique hash w/o being made a lot slower, then I'd 
> think your new version would be faster in the general case (at least for 
> char[] AA keys).

How have you tested the number of collisions? It would be fairly trivial 
to add a counter to aaA.d to keep track of the number of collisions.

The size of the AA's internal array is the biggest factor, I've noticed. 
Depending on the benchmark, the new AA might end-up having either a much 
bigger or smaller array than the old AA, and therefor much less/more 
collisions. To correctly test the two implementations, we should either 
add a collision metric (testing the hashing quality) or only compare 
arrays of similar size (which is not possible given the way the two 
implementations resize).

A good thing of the new AA is the fact that it's sizing can be more 
easily controlled, and the AA could, in theory, be easily tuned for the 
task at hand. The old AA uses that static list of primes (half of which, 
in fact, are never being used; see thread in D.learn).

I'll try using the string hash method from java "h=7; foreach(b;a) 
h=(h<<5)+b-h;" but I doubt that there'll be any useful result. Although, 
one less multiplication might make it faster, still ;)

L.



More information about the Digitalmars-d mailing list