gpt4 book ai didi

c# - Dictionary.ContainsKey/Value 和检查某个键/值的 foreach 循环之间的速度是否存在差异

转载 作者:太空狗 更新时间:2023-10-30 00:09:18 24 4
gpt4 key购买 nike

Dictionary.ContainsKey/Value 和检查特定键/值的 foreach 循环在速度上有区别吗?

最佳答案

ContainsKey更快:

This method approaches an O(1) operation.

ContainsValue就像一个 foreach 循环。

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.

关于c# - Dictionary.ContainsKey/Value 和检查某个键/值的 foreach 循环之间的速度是否存在差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1913376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com