开发者

Is there difference in speed between Dictionary.ContainsKey/Value and a foreach loop that checks for a certain key/value

开发者 https://www.devze.com 2022-12-14 16:11 出处:网络
Is there difference in speed between Dictionary.ContainsKey/Valu开发者_如何学Goe and a foreach loop that checks for a certain key/value?ContainsKey is faster :

Is there difference in speed between Dictionary.ContainsKey/Valu开发者_如何学Goe and a foreach loop that checks for a certain key/value?


ContainsKey is faster :

This method approaches an O(1) operation.

ContainsValue is like a foreach loop.

This method performs a linear search; therefore, the average execution time is proportional to Count. That is, this method is an O(n) operation, where n is Count.


Yes.

ContainsKey is nearly O(1). As for ContainsValue, I can't tell for sure, but I think there won't be much difference to a loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消