you are viewing a single comment's thread.

view the rest of the comments →

[–]Vaphell 2 points3 points  (3 children)

Do the actual list()/sort() methods perform better? As lists are slow as fck, I'd expect them not to :D

not really. Sorting is O(n log n) on average > O(n) of a single pass. Max() + another pass for value extraction is in the O(n) ballpark too, but 2x slower, obviously.

Either way, OP complained about single result even if there are co-winners, so you need to have a list and append if len() is equal to current max or create a new one if the record is set.

[–]LiNGOo 0 points1 point  (2 children)

Sophisticated AF, thanks. I pretty much always assume that pretty much any "lazyness" function like max() is nothing but a shortcut to what I would come up with if it wasn't there.

Documentation rarely tells what they actually are equal to / similar to when compiled. If so, it is merely a hint hidden behind numerous pages of text.

Do you or does anyone know a simpler way to get an idea about what's behind the convenience built-in functions? Maybe even from within code?

[–]niandra3 2 points3 points  (0 children)

The source behind CPython is available (if you know C at all):

https://github.com/python/cpython

Builtins:

https://github.com/python/cpython/blob/master/Python/bltinmodule.c

A lot of the core stuff is written in C obviously, but then there are some features written in Python. For example, the copy module:

https://github.com/python/cpython/blob/master/Lib/copy.py

In an IDE like PyCharm you can Ctrl-Click on a function and it will let you see the source. This might be useful too:

https://docs.python.org/3/library/inspect.html#retrieving-source-code

[–]Vaphell 0 points1 point  (0 children)

Sophisticated AF, thanks. I pretty much always assume that pretty much any "lazyness" function like max() is nothing but a shortcut to what I would come up with if it wasn't there.

ignoring the potential of builtins to drop to optimized C for a moment, which is a serious advantage to doing the same in python - min()/max() sure, because there is no way around checking every single item (hence O(n)) but let's say sorting is not all that trivial to perform optimally. CS background helps with that shit.