Complexity of len() with regard to sets and lists

前端 未结 7 1323
耶瑟儿~
耶瑟儿~ 2021-02-04 23:38

The complexity of len() with regards to sets and lists is equally O(1). How come it takes more time to process sets?

~$ python -m timeit \"a=[1,2,3,         


        
7条回答
  •  悲哀的现实
    2021-02-05 00:05

    Remove the len(a) statement. The result is pretty much the same. A set needs to be hashed to retain only distinct items so it's slower.

提交回复
热议问题