Why is std::unordered_map slow, and can I use it more effectively to alleviate that?

后端 未结 1 850
情歌与酒
情歌与酒 2020-12-03 15:39

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

相关标签:
1条回答
  • 2020-12-03 16:13

    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.

    0 讨论(0)
提交回复
热议问题