helios wrote: |
---|
It's difficult to make blanket statements about when a hash table is faster than an array. I would certainly not make them here, since OP's question doesn't even make sense. |
My post was about what
jonin wrote, not the OP. He was contending that a hash might be better (search in O(1) ), I was relaying a
real and timed example where it wasn't. More importantly, the point was that hash tables have been considered (in theory) to be efficient in CS, but that isn't necessarily the case with modern C++, and I relayed the reasons why.
Stroustrup has said that "it is only real C++ experts that can say about the real efficiency of STL containers". It's not enough to say that a hash function will run in O(1), so that is better than something else. It was
JLBorges who demonstrated all this to me, along with another discussion with
Cubbi and
mbozzi
So this raises another question: Why would someone want to use an STL hash container like
std::unordered_set
, given the big difference in performance shown in the example above? I could guess: Maybe their data has duplicates; or they implemented a different strategy for the hashing; or some other reason.
Cubbi mentioned that he has experienced several times where they rolled their own hash tables / functions. (No pun intended :+) )
Here is the link to the example again, save scrolling up to look for it:
http://www.cplusplus.com/forum/general/193311/#msg930382
Edit:
I guess a hash table would be handy for a DB table, where there are multiple different indexes. One wouldn't want to be resorting on different fields all the time. I guess the user would have to put up with the
embuggerance embotherance if the container is growing rapidly.
I should also say that I don't have anything against
jonin, I was relating what I had learnt - maybe that wasn't the entire picture, Regards :+)