Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Doing constant modulo on an integer is very fast - we're talking a few CPU operations.


It won't be a compile-time constant unless your hash table is incapable of resizing. And modulo arithmetic on arbitrary values is slow (20-94 cycles of latency on Sandy Bridge).


Hmm, good point. Is that also true for JIT compilers?


Yes, but bitwise AND is faster.


Cross that bridge when you get there. If you're really finding that your hash function is the slowest piece of code in a tightly bound for-loop, and it's not all the collisions you're having (from having a bad hashing function), then look into alternatives. Before that, you're doing premature optimization.


Please read the whole thread before replying with the "premature optimization" thing. We are talking about hash table optimizations; this commenter -- http://news.ycombinator.com/item?id=4037320 -- is asking about the cost of modulo operation.


IIrc changing a modulo to an and roughly doubled the performance of my hashtable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: