Add a page detailing the time complexity of operations on built-in types - #154363
Add a page detailing the time complexity of operations on built-in types#154363StanFromIreland wants to merge 14 commits into
Conversation
Documentation build overview
73 files changed ·
|
picnixz
left a comment
There was a problem hiding this comment.
Iteration is, AFAICT, always O(n) so we can group that under a common table. Likewise, getting the length of a sized object is O(1).
I think it's better to keep pythonic exmaples as well. "d[key]" is mucher better than "get item" IMO.
When I read "the non-mtating operations below", it also rings "the opertaions below are all non-mutating". Instead, I suggest to add some * if the opertaion is mutating and explain that at the beginning of the document.
While it would make the tables shorter, it's not so straightforward unfortunately. There are subtle differences, see footnote six. I also think that reference pages are scanned, not read linearly. As such, I'd prefer to keep them where they are. |
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
eendebakpt
left a comment
There was a problem hiding this comment.
I am +0.5 in this. I like to the idea, but it will take time to maintain and it is not clear to me where to draw the line when more additions are requested (e.g. memory complexity, alternatives, more detailed descriptions, more references).
|
FTR the Qt help pages once had a page for complexty of operations of their STL containers, so it might be worth reading them if you need precedents in other languages. I do not know if those pages still exist as they were available for Qt5 dirdctly in the Qt Creator IDE. |
|
I found back the link: https://doc.qt.io/qt-6/containers.html#algorithmic-complexity |
|
Thanks Pieter, Bénédikt and Hugo for the reviews ❤️ ! |
|
@StanFromIreland I really appreciate the work you've put into this, and the bravery of taking it on! My comments are minor. I'm looking forward to this getting merged. Do we already have a clear idea of what to do with the other data structures in the stdlib? This page directs people to deques, but doesn't include them (I know, because they are not a built-in). |
Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
nedbat
left a comment
There was a problem hiding this comment.
One last fix, then it's good from my side.
|
@dg-pb maybe this is something you'd be interested in reviewing? :-) |
|
Substring search: might be worth mentioning that reverse search operations have O(n^2) worst case as So Apart from this, LGTM. Thank you for this work - I know I will enjoy visiting this page. |
Yep! I also came across that while writing this page :-)
See footnote 10, although I ended up with O(nk) worst case. |
|
Seen it: "A naive substring search would need O(nk) comparisons in the worst case, where k is the length of the substring searched for, but CPython uses search algorithms with a linear worst case for forward searches." Yes, it does (maybe?) encode this information. However, this can be misinterpreted:
I think this is important and personally, I would add another line in the table -- this is not "by-the-way", this is a real risk. It doesn't seem to cause many issues due to infrequent usage, but I think being in-your-face-explicit about this would be an honest thing to do. |
Co-authored-by: dgpb <3577712+dg-pb@users.noreply.github.com>
|
I see, that's reasonable, I've sent 4d223f1. |
Co-authored-by: Ned Batchelder <ned@nedbatchelder.com> Co-authored-by: dgpb <3577712+dg-pb@users.noreply.github.com>
| input. Forward searches instead use a more elaborate algorithm with a | ||
| linear worst case, described in | ||
| :source:`Objects/stringlib/stringlib_find_two_way_notes.txt`. | ||
| ``s.rpartition(x)`` and ``s.rsplit(x)`` search backwards too, with the same |
There was a problem hiding this comment.
rpartition and rsplit are mentioned in this footnote, but partition and split aren't mentioned anywhere.
Inspired by @nedbat's post on Discourse:
The page's location isn't ideal, but we don't have a better place for it currently. We discussed this at the last Docs Community meeting, but didn't come up with a better place to put it, and creating new top level sections is in my opinion, out of scope (as we'd need to move other pages as well). If it's desired we can do it in a future PR.
I spend a while considering how to best present time complexity here, avoiding complexity ;-) The wiki pages splits it into amortised, worst, best and averages cases (each type had a selection of those), but I think that it would be too confusing for readers. As such I give the average case, and put the details in the prose/footnotes.
The numbers are, hopefully, correct, as I don't think I missed any quirks (at least I don't know of any ;-). But I'd appreciate a review from the experts here, maybe Serhiy or Tim could please take a peek?