www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - getHash inconsistency

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
Is this a bug?

	char[] a = "abc".dup;
	const(char)[] b = "abc";
	string c = "abc";

	writeln(typeid(a).getHash(&a));	// 12914
	writeln(typeid(b).getHash(&b)); // 8503969683799911018
	writeln(typeid(c).getHash(&c));	// 12914


T

-- 
Obviously, some things aren't very obvious.
Mar 19 2012
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"H. S. Teoh" <hsteoh quickfur.ath.cx> wrote in message 
news:mailman.910.1332214803.4860.digitalmars-d puremagic.com...
 Is this a bug?

 char[] a = "abc".dup;
 const(char)[] b = "abc";
 string c = "abc";

 writeln(typeid(a).getHash(&a)); // 12914
 writeln(typeid(b).getHash(&b)); // 8503969683799911018
 writeln(typeid(c).getHash(&c)); // 12914

Of course.
Mar 19 2012
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/22/12 1:18 PM, H. S. Teoh wrote:
 Alright, so after some benchmarking, I found that the above custom hash
 function works best for *short* (3 to 10 character) randomized
 alphabetic strings (I assumed alphabetic to be the typical use case of
 strings). It's faster than SuperFastHash, and even has better
 distribution properties, probably because SuperFastHash is tuned for
 arbitrary binary data of arbitrary length, whereas the custom function
 is tuned for short string-like data.

 With longer strings, SuperFastHash beats the custom algorithm, and
 distribution properties are approximately the same.

 So I'm still on the fence about which algorithm is better. I can see why
 the custom hash was adopted for strings, since your typical AA tends to
 have short alphabetic keys, and something like SuperFastHash is probably
 overkill. But for longer keys, SuperFastHash is better.

Note that you can switch the actual algorithm depending on string length. Andrei
Mar 22 2012