in reply to Re^3: elsif chain vs. dispatch
in thread elsif chain vs. dispatch
To pre-size a hash or force it get bigger, assign a scalar to keys %hash, eg: keys %hash=8192;.
The Perl hash algorithm is:
Perl cuts the above value to the hash array size of bits, which in Perl is always a power of 2. As mentioned above, this "(10/1024)" string shows number of "buckets" and total "buckets". There is another value, hxv_keys accessible to the Perl "guts" that contains the total number of hash entries./* of course C code */ int i = klen; unsigned int hash =0; char *s = key; while (i--) hash = hash *33 + *s++;
If the total number of (typo update:) keysentries exceeds the number of buckets used, Perl will increase the hash size by one more bit and recalculate all hash keys again.
So let's say that we have a hash with 8 buckets and for some reason only one of those buckets is being used. When the ninth one shows up, Perl will see (9>8) and will re-size the hash by adding one more bit to the hash key. In practice, this algorithm appears to work pretty well. I guess there are some improvement in >Perl 5.8.3.
Anyway I often work the hashes with say 100,000 things and haven't seen the need yet to override the Perl hash algorithm.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: elsif chain vs. dispatch
by ikegami (Patriarch) on Apr 27, 2009 at 20:34 UTC | |
by Marshall (Canon) on Apr 27, 2009 at 21:02 UTC | |
by ikegami (Patriarch) on Apr 27, 2009 at 21:16 UTC | |
by JavaFan (Canon) on Apr 27, 2009 at 22:40 UTC | |
by Marshall (Canon) on Apr 27, 2009 at 23:30 UTC | |
by ikegami (Patriarch) on Apr 27, 2009 at 23:36 UTC | |
| |
by JavaFan (Canon) on Apr 28, 2009 at 00:14 UTC | |
| |
by Marshall (Canon) on Apr 27, 2009 at 21:54 UTC |