【发布时间】:2019-08-26 05:25:09
【问题描述】:
在collections.deque上调用Python内置的len()函数的时间复杂度是多少?我希望它是 O(1),但我还没有找到任何证实这一事实的证据。
【问题讨论】:
-
至少在 CPython 中,见the defintion of
deque_len,其中Py_SIZE(deque)只是一个macro around a field reference。我发现其他实现不太可能让它变慢。 -
答案是 O(1):stackoverflow.com/a/1115382/10397775
标签: python collections deque