Live data from Hacker News

Python sets and dictionaries can have quadratic-time performance

lemire.me

21–30 of 67 posts

Re: Python sets and dictionaries can have quadratic-time performance

#21
post #7

> 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.

People … say that all the time. *I* say that all the time. It’s true enough to be accurate in 99.9% of the cases; and we put barriers in place when implementing code (like configuring the hashing algorithm) to keep it that way.

Re: Python sets and dictionaries can have quadratic-time performance

#22
post #7

> 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.

O(1) insertion is the amortized worst-case time complexity, actually. (Amortized in the sense that the O(n) cost of copying is paid only during the n-th insertion). Average complexity is a slightly different thing.

Re: Python sets and dictionaries can have quadratic-time performance

#23
Raymond Hettinger has a great talk about how much python's dict has improved over the years. So this is super interesting and will probably just make the builtin dict better eventually.

The lesson of the talk is that if you are idiomatic then you will benefit as the language improves.

https://www.youtube.com/watch?v=npw4s1QTmPg

Re: Python sets and dictionaries can have quadratic-time performance

#24
post #18

Earlier 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

you might want to read that chapter of CLRS again

Re: Python sets and dictionaries can have quadratic-time performance

#26
post #4

Java'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)).

One get can O(1) expected time on any set of keys if one uses "universal hashing": choosing the hash function at random from a universal set of hash functions. The expectation is now over this random choice, not over some random distribution of key inputs. So even if an adversary gets to choose the keys the expected behavior is good.

https://en.wikipedia.org/wiki/Universal_hashing

For hashing with chaining, the hash function just has to make the hash values of keys pairwise independent to achieve O(1) expected time per operation; higher order independence is not needed.

Re: Python sets and dictionaries can have quadratic-time performance

#28
Most algorithm analyses don't incorporate memory hierarchies. I'm not sure what the point of this post was.

If you are really concerned about it, compute the empirical roofline for your machine.

Also, if the point is to point out memory hierarchies, it's not 'quadratic performance.' The algorithm doesn't behave differently once it spills over. The costs just get bigger.

Post reply on HN