[issue46192] Optimize builtin functions min() and max()

Erlend E. Aasland report at bugs.python.org
Thu Dec 30 08:06:30 EST 2021


Erlend E. Aasland <erlend.aasland at innova.no> added the comment:

Repeating my comment on GH-30286: If we are touching `min()` and `max()`, it would make sense, IMO, to port them to Argument Clinic. FTR, Argument Clinic got `*args` support in GH-18609 / bpo-20291.

@colorfulappl made a "competing" branch using AC on his local fork[^1]. However, that branch contained a bug with the key function; I made an amended version[^2] for benchmarking. Here's some micro-benchmarks from optimised builds on macOS 12.1 w/Clang 13:

```
+---------------------------------------------------------+---------+-----------------------+----------------------+
| Benchmark                                               | main    | GH-30286              | GH-30286-ac          |
+=========================================================+=========+=======================+======================+
| max(a, b)                                               | 193 ns  | 74.1 ns: 2.60x faster | 179 ns: 1.08x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max(a, b, c, d, e)                                      | 273 ns  | 126 ns: 2.17x faster  | 260 ns: 1.05x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max([a, b])                                             | 267 ns  | 185 ns: 1.44x faster  | 239 ns: 1.12x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max([a, b, c, d, e])                                    | 345 ns  | 259 ns: 1.33x faster  | 312 ns: 1.10x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max((a,), (b,), key=lambda x: x[0])                     | 707 ns  | 444 ns: 1.59x faster  | 513 ns: 1.38x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max((a,), (b,), (c,), (d,), (e,), key=lambda x: x[0])   | 1.12 us | 831 ns: 1.35x faster  | 930 ns: 1.20x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max([(a,), (b,)], key=lambda x: x[0])                   | 786 ns  | 561 ns: 1.40x faster  | 570 ns: 1.38x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max([(a,), (b,), (c,), (d,), (e,)], key=lambda x: x[0]) | 1.19 us | 981 ns: 1.22x faster  | 981 ns: 1.22x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| max([], default=-1)                                     | 484 ns  | 177 ns: 2.73x faster  | 188 ns: 2.57x faster |
+---------------------------------------------------------+---------+-----------------------+----------------------+
| Geometric mean                                          | (ref)   | 1.68x faster          | 1.29x faster         |
+---------------------------------------------------------+---------+-----------------------+----------------------+
```



[^1]: https://github.com/colorfulappl/cpython/commit/29b9559de31ae19e8d127d7a3063494b2d9791b0
[^2]: https://github.com/erlend-aasland/cpython/tree/gh-30286-ac

----------
nosy: +erlendaasland
Added file: https://bugs.python.org/file50528/bench-minmax.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46192>
_______________________________________


More information about the Python-bugs-list mailing list