accessing char as int through union

  • Thread starter Hallvard B Furuseth
  • Start date
C

CBFalconer

Hallvard said:
Thanks to several people for answers and explanations, in particular
websnarf. A few details:


.... snip ...

Actually that was quite wrong, I seem to have counted a test sample
instead or something at a late night. 5-6 letters is more like it
for some inputs, ~10 for others.


Aah, of course.

Of course the overhead of splitting off the long strings, and thus
of calling strlen on them, will probably overshadow any gains,
especially when the expected lengths are short. You can't really
say anything without making measurements on the actual data.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
C

CBFalconer

Hallvard said:
Sorry, I meant I knew the low quality slowed down the hash lookup,
I hadn't realized it was actually slow by itself, for a hash
function. (Due to the extra strlen needed elsewhere.)

That function can compute the length as a side effect. All you
have to do is return it.

unsigned long hash(char *s, size_t *lgh) {
unsigned long h;
char *p;

h = 0; p = s;
while (*p) h = 33 * h + (unsigned char)*p++;
*lgh = p - s;
return h;
}

31 and 37 are also suitable multipliers. Apply KISS.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,298
Latest member
ZenLeafCBDSupplement

Latest Threads

Top