Python sets and dictionaries can have quadratic-time performance
11–20 of 67 posts
Re: Python sets and dictionaries can have quadratic-time performance
#12Java's HashMap also has O(log(N)) complexity on hash collision, and that is before memory/cache details. https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.... In fact, some studying on data structures probably leads to the conclusion that it is impossible to guarantee that an unbounded set/map to have access performance under O(log(N)).
Expected
Granted, one can technically call that O(log(n)), but that's not a helpful categorization.
Re: Python sets and dictionaries can have quadratic-time performance
#13Edit: A charitable take is constructing a set/dict from a list is indeed a common operation so it's worthwhile to think about its complexity, but it's not really one of the standard operations when discussing the performance of a hashset/hashmap, so really shouldn't be this handwavy.
And instead of attacking some straw man "It is indeed widely believed that ..." claim (widely believed by who?), why not attack what's literally on docs.python.org? https://docs.python.org/3/library/time-complexity.html:
> dict
> The times listed for dict objects are average-case times, as they assume the hash function for the objects is sufficiently robust to make collisions uncommon. They also assume the keys are well-distributed among the set of possible keys. In the worst case, when every key hashes to the same value, each of the O(1) operations below instead takes O(n) time. They also assume that hashing and comparing a key is O(1). For more detail on the implementation, see How are dictionaries implemented in CPython?.
> ...
> set, frozenset
> See dict as the set and frozenset implementations are similar, and the same caveats apply. In the worst case, O(1) operations instead take O(n) time, and operations that look up every element degrade accordingly.
+--------------------------------------+------------+
| Operation | Complexity |
+--------------------------------------+------------+
| x in s | O(1) |
| Copy (s.copy()) [6] [7] | O(n) |
| Add (s.add(x)) [1] | O(1) |
| Discard (s.discard(x), s.remove(x)) | O(1) |
| ... | ... |
+--------------------------------------+------------+
You explicitly construct a list of ints that are all multiples of sys.hash_info.modulus and hence all hash to 0, no shit you get that well documented O(n) behavior.The discussion of CPU cache is good though, so why hide that behind this clickbait.
Re: Python sets and dictionaries can have quadratic-time performance
#14Re: Python sets and dictionaries can have quadratic-time performance
#15Re: Python sets and dictionaries can have quadratic-time performance
#16Re: Python sets and dictionaries can have quadratic-time performance
#17Why is M so big? Why does it cross the maxint boundary? Why is constructing the list comprehension part of the benchmark? Why are we summing the set? Why are we only measuring 5 values for n?
Re: Python sets and dictionaries can have quadratic-time performance
#18> To put it differently, saying that a hash table is O(1) or constant time is a model Nobody really says that, nor is it a model. It is the expected time complexity.
I think people do say a hash table is O(1). It's the average time complexity (for some value of average) though, not the worst case.
Re: Python sets and dictionaries can have quadratic-time performance
#19Earlier quoted context omitted.
I think people do say a hash table is O(1). It's the average time complexity (for some value of average) though, not the worst case.
Yeah but there's a formal term for average time complexity, Theta
Re: Python sets and dictionaries can have quadratic-time performance
#20Earlier quoted context omitted.
Expected
Nowadays I expected an opaque dictionary to be amortized O(1). Granted, one can technically call that O(log(n)), but that's not a helpful categorization.