A faster replacement to the Dictionary

后端 未结 10 875
醉梦人生
醉梦人生 2020-12-08 15:03

I need a fast replacement for the System.Collections.Generic.Dictionary. My application should be really fast. So, the repl

10条回答
  •  盖世英雄少女心
    2020-12-08 15:19

    USE INTS AS KEYS FOR MAXIMUM PERFORMANCE:

    For anyone who came here from Google, if you want to squeeze every last bit of performance out of a Dictionary, then use Ints as keys. Here's a benchmark comparing Int vs String Keys: https://jacksondunstan.com/articles/2527

    The author of the article even mentions that converting strings to ints is worthwhile if you have such a need.

    Also, note that this same behavior occurs in some other languages like PHP. Php associative arrays -are in fact- dictionaries, and if you use Ints in ascending order in PHP7, they outperform string keys tremendously.

提交回复
热议问题