There is a speed difference between Dictionary.ContainsKey / Value and a foreach loop that checks for a specific key / value

is there any difference in speed between Dictionary.ContainsKey / Value and the foreach loop that checks for a specific key / value?

+3
source share
2 answers

ContainsKey is faster:

This method approaches operation O (1).

ContainsValue as a foreach loop.

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

+9
source

Yes.

ContainsKey O (1). ContainsValue, , , .

+4

Source: https://habr.com/ru/post/1725821/


All Articles