Speed-up math.dist() by 30% by rhettinger · Pull Request #9628 · python/cpython (original) (raw)
Manual type checking is much faster than the argument clinic equivalent.
--------- baseline ------------
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 95.5 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 95.3 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 95.5 nsec per loop
------ patched ------
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 67 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 67.2 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 67.3 nsec per loop