Which performance have cPython sets in comparison to lists?
I have just found these performance notes for cPython lists:
Time needed for python lists to ....
- ... get or set an individual item: O(1)
- ... append an item to the list: worst O(n^2), but usually O(1)
- ... insert an item: O(n), where n is the number of elements after the inserted one
- ... remove an ite开发者_开发技巧m: O(n)
Now I would like to know the same performance characteristics for cPython sets. Also, I would like how fast iteration over the list / set is. I am especially interested in large lists / sets.
AFAIK, the Python "specification" does not impose specific data structures for implementation of lists, dictionaries or sets, so this can't be answered "officially". If you're only concerned about CPython (the reference implementation), then we can throw in some un-official complexities. You might want to re-formulate your question to target a specific Python implementation.
In any case, the complexities you mentioned can't be right. Supposing a dynamically-resized array implementation, appending an item is amortized O(1)
: most often you simple copy the new value, and in the worst case, you need to re-allocate, copying all n
items, plus the new one. Next, inserting has exactly the same worst case scenario, so it has the same upper bound on complexity, but in the best case, it only moves k
items, where k
is the number of items past the position where you're inserting.
精彩评论