🔒 Locked
String hash code efficiency
Advertisement
Original Post
Hi ! I was discussing a few weeks ago with a friend of mine about how to minimize string memory allocation/de-allocation at run-time ( during a game main loop for exemple ) He told me that the solution he was using at his previous company was to compute a CRC or an Hash-Code from the string and to use this code to work with those string as often as possible ( identification, comparation and so on )... After a few thaught at the corner of my desk i decided to go that way and to use a similar approach in my engine. So i google for some time and found the following hash function for string: StringHash CString::GetHashCode() const { Int32 hash = 0; for (Int32 i = 0; i < m_Lenght; i++) { hash = hash * 31 + m_Buffer; } return hash; } Actually everything work fine, but i still have one question left : What is the percentage of chance to get the same code for two different strings ?? The strings that i'm using are mainly resources identifier, so the string length will not exceed 20 characters ... Thanks Clement Vidal
So it's absolutly not a good solution to use such a method apprently ...
Do you think the problem can come from my hash function ??
Is there any better for those kind of string ?
Do you know any other approach to reduce memory allocation/deallocation of string at runtime?
Thanks
Do you think the problem can come from my hash function ??
Is there any better for those kind of string ?
Do you know any other approach to reduce memory allocation/deallocation of string at runtime?
Thanks
Quote:
Original post by ClementLuminy
What is the percentage of chance to get the same code for two different strings ??
The strings that i'm using are mainly resources identifier, so the string length will not exceed 20 characters ...
Entropy of the crc is 32 bits.
As such, it can represent 2^32 unique values.
Entropy of all strings could be 8*20, but if you consider a alphanumeric alphabet, it's somewhere around 6 bits per character, so 120 bits. To be guaranteed unique crcs, you'd need 128 bit crc (or just forget the crc and just use the string as key).
Hash collisions shouldn't be a problem for small sets (tens of thousands of filenames). But when they do happen, you will need to modify the filenames.
The thing about string hashes is that you use them to check if two strings are not equal. It's guaranteed that if the hashes are different than so are the strings.
For testing for equality you first test for inequality with the hash (since it's super fast), then if that fails fall back to the full check.
For testing for equality you first test for inequality with the hash (since it's super fast), then if that fails fall back to the full check.
[deleted by user.]
Though I had something to say, but when I thought about it, it was irrelevant.
Though I had something to say, but when I thought about it, it was irrelevant.
This one should be pretty good. Search for DJBX33A.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement