logoalt Hacker News

Time complexity of operations on Python's built-in types

97 pointsby theanonymousonelast Tuesday at 3:15 PM38 commentsview on HN

Comments

alexpotatoyesterday at 11:47 AM

Dave Beazley has a great talk about using Python built ins [0] for data analysis and other quick operations.

As a meta note, I've used many of these builtins over the years but, due to LLMs, have been using them less and less. Re-watching the video almost felt like watching bushcrafters make a chair using just a knife and saw...

0 - https://www.youtube.com/watch?v=lyDLAutA88s

StellarScienceyesterday at 3:42 PM

Python is famously built around hash tables. So much so that several versions ago they made an improvement to the hash table implementation, and the entire language became several percent faster.

However, I'm surprised to see no data structures at all with O(log(N)) complexity. Surely there are some use cases for which that's desirable?

show 1 reply
Retr0idyesterday at 5:10 PM

Excellent. Previously this was only documented semi-unofficially on the wiki here: https://wiki.python.org/moin/TimeComplexity

wodenokotoyesterday at 10:49 AM

Why are `min(r)` and `max(r)` for range objects o(n) ?

I thought min and max where constants stored in the object. Basically you are just asking for one of the parameters it was created with.

show 2 replies
mwkaufmayesterday at 5:54 PM

Isn't O(n - k) or O(len(l1) + len(l2)) just O(n)? Instead of blurring the line between complexity-analysis and cycle-counting, just print both the complexity and the est proportional cycle-count as separate measures.

show 4 replies
gpugregyesterday at 11:02 AM

Notable pitfalls:

- s[i:j] is O(j - i) because it creates a copy instead of a view

- max(range(n)) is O(n)

- substring search is O(n), which is good, but rfind is O(n m)

- iterative string concatenation (for c in ...: s += c) can be O(n^2) due to string immutability according to footnote 10, although it is O(n) in most cases due to an implementation detail of CPython: https://stackoverflow.com/a/34008199

show 2 replies
emil-lpyesterday at 2:01 PM

They forgot to include GC overhead.

show 1 reply
jjgreenyesterday at 11:36 AM

Nice page, but odd that they have O(...) in every row, surely that belongs in the column header