[New-bugs-announce] [issue29735] Optimize functools.partial() for positional arguments
STINNER Victor
report at bugs.python.org
Mon Mar 6 08:22:46 EST 2017
New submission from STINNER Victor:
The pull request makes functools.partial() faster for positional arguments. It avoids the creation of a tuple for positional arguments. It allocates a small buffer for up to 5 parameters. But it seems like even if the small buffer is not used, it's still faster.
Use small buffer, total: 2 positional arguments.
haypo at smithers$ ./python -m perf timeit -s 'from functools import partial; f = lambda x, y: None; g = partial(f, 1)' 'g(2)' --duplicate=100 --compare-to ../master-ref/python --python-names=ref:patch --python-names=ref:patch
ref: ..................... 138 ns +- 1 ns
patch: ..................... 121 ns +- 1 ns
Median +- std dev: [ref] 138 ns +- 1 ns -> [patch] 121 ns +- 1 ns: 1.14x faster (-12%)
Don't use small buffer, total: 6 positional arguments.
haypo at smithers$ ./python -m perf timeit -s 'from functools import partial; f = lambda a1, a2, a3, a4, a5, a6: None; g = partial(f, 1, 2, 3, 4, 5)' 'g(6)' --duplicate=100 --compare-to ../master-ref/python --python-names=ref:patch --python-names=ref:patch
ref: ..................... 156 ns +- 1 ns
patch: ..................... 136 ns +- 0 ns
Median +- std dev: [ref] 156 ns +- 1 ns -> [patch] 136 ns +- 0 ns: 1.15x faster (-13%)
Another benchmark with 10 position arguments:
haypo at smithers$ ./python -m perf timeit -s 'from functools import partial; f = lambda a1, a2, a3, a4, a5, a6, a7, a8, a9, a10: None; g = partial(f, 1, 2, 3, 4, 5)' 'g(6, 7, 8, 9, 10)' --duplicate=100 --compare-to ../master-ref/python --python-names=ref:patch --python-names=ref:patch
ref: ..................... 193 ns +- 1 ns
patch: ..................... 166 ns +- 2 ns
Median +- std dev: [ref] 193 ns +- 1 ns -> [patch] 166 ns +- 2 ns: 1.17x faster (-14%)
----------
messages: 289100
nosy: haypo
priority: normal
severity: normal
status: open
title: Optimize functools.partial() for positional arguments
type: performance
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29735>
_______________________________________
More information about the New-bugs-announce
mailing list