[New-bugs-announce] [issue31410] int.__repr__() is slower than repr()
Serhiy Storchaka
report at bugs.python.org
Sun Sep 10 13:34:20 EDT 2017
New submission from Serhiy Storchaka:
Following the StackOverflow question [1].
Calling repr() is faster than calling unbound method __repr__(). This looks strange at first glance because it is *obvious* that repr() is implemented via calling __repr__().
$ ./python -m timeit "''.join(map(repr, range(10000)))"
500 loops, best of 5: 809 usec per loop
$ ./python -m timeit "''.join(map(int.__repr__, range(10000)))"
200 loops, best of 5: 1.27 msec per loop
Actually repr() just called the tp_repr slot, while calling int.__repr__ passes through many intermediate layers.
Proposed PR gets rid of a half of the overhead. It avoids creating and calling an itermediate function object. The result still is slower then calling repr().
$ ./python -m timeit "''.join(map(int.__repr__, range(10000)))"
200 loops, best of 5: 1.01 msec per loop
The PR also speeds up calling classmethod descriptors.
$ ./python -m timeit -s "cm = bytes.fromhex; args = [('',)]*10000; from itertools import starmap" -- "b''.join(starmap(cm, args))"
500 loops, best of 5: 515 usec per loop
$ ./python -m timeit -s "cm = bytes.__dict__['fromhex']; args = [(bytes, '')]*10000; from itertools import starmap" -- "b''.join(starmap(cm, args))"
500 loops, best of 5: 704 usec per loop
Patched:
$ ./python -m timeit -s "cm = bytes.__dict__['fromhex']; args = [(bytes, '')]*10000; from itertools import starmap" -- "b''.join(starmap(cm, args))"
500 loops, best of 5: 598 usec per loop
[1] https://stackoverflow.com/questions/45376719/why-is-reprint-faster-than-strint
----------
components: Interpreter Core
messages: 301821
nosy: haypo, serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: int.__repr__() is slower than repr()
type: performance
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31410>
_______________________________________
More information about the New-bugs-announce
mailing list