[issue26506] hex() documentation: mention "%x" % int
STINNER Victor
report at bugs.python.org
Wed Mar 9 18:16:57 EST 2016
STINNER Victor added the comment:
> Ah, but it's not that format() is slower in 3.5, but that %-formatting got faster.
Hum, python3 looks faster on this dummy microbenchmark yeah. Who said that Python 3 is slower? :-)
$ python2 -m timeit -s 'import random; nums = [random.randint(0, 255) for n in range(10**5)]' '["%x" % x for x in nums]'
10 loops, best of 3: 43.7 msec per loop
$ python3 -m timeit -s 'import random; nums = [random.randint(0, 255) for n in range(10**5)]' '["%x" % x for x in nums]'
10 loops, best of 3: 19.2 msec per loop
I spent a lot time to micro-optimize str%args, str.format(args), and operations on str in general in Python 3. I wrote a first article to explain my work on optimization:
https://haypo.github.io/pybyteswriter.html
I have a draft article explaning other kinds of optimizations related to the PEP 393.
> It looks as if it got optimized and I was wondering whether the same optimization could be applied to format().
str.format(args) was also optimized, but it's still faster than str%args.
On Python 3, "%x" % 0x1234abc takes 17 nanoseconds according to timeit. It's super fast! Any extra work can have a non negligible overhead. For example, it's known that operators are faster than functions in Python. One reason is that a calling requires to lookup the function in namespaces (local, global or builtin namespaces). It can be even worse (slower) to lookup a method (especially with custom __getattr__ method).
--
Hum, I don't recall why you started to talk about performance :-D
Why not documenting "%x" % value *and* format(value, 'x')?
I prefer "%x" % value. I never use format(value, ...) but sometimes I use "{0:x}".format(value).
f'{x:value}' looks too magical for me.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26506>
_______________________________________
More information about the Python-bugs-list
mailing list