I’ve recently found out an odd thing. It seems that calculating Collatz sequence lengths with no caching at all is over 2 times faster than using std::unordered_map
The standard library's maps are, indeed, inherently slow (std::map
especially but std::unoredered_map
as well, with its linked-list bucket). Google's Chandler Carruth explains this in his CppCon 2014 talk; in a nutshell: std::unordered_map
is cache-unfriendly because it uses linked lists as buckets.
This SO question mentioned some efficient hash map implementations - use one of those instead.