Which is faster/more efficient: Dictionary or Dictionary?

后端 未结 4 1018
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 16:27

Are enum types faster/more efficient than string types when used as dictionary keys?

IDictionary or IDict         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 16:43

    I think you should start by focusing on correctness. This is far more important than the minimal difference between the minor performance differences that may occur within your program. In this case I would focus on the proper representation of your types (enum appears to be best). Then later on profile your application and if there is a issue, then and only then should you fix it.

    Making code faster later in the process is typically a straight forward process. Take the link that skolima provided. If you had chosen enum, it would have been a roughly 10 minute fix to remove a potential performance problem in your application. I want to stress the word potential here. This was definitely a problem for NHibernate but as to whether or not it would be a problem for your program would be solely determined by the uses.

    On the other hand, making code more correct later in the process tends to be more difficult. In a large enough problem you'll find that people start taking dependencies on the side effects of the previous bad behavior. This can make correcting code without breaking other components challenging.

提交回复
热议问题