This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients docs@python, eric.smith, vstinner, wolma
Date 2016-03-09.23:16:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1457565417.41.0.0556401980322.issue26506@psf.upfronthosting.co.za>
In-reply-to
Content
> 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.
History
Date User Action Args
2016-03-09 23:16:57vstinnersetrecipients: + vstinner, eric.smith, docs@python, wolma
2016-03-09 23:16:57vstinnersetmessageid: <1457565417.41.0.0556401980322.issue26506@psf.upfronthosting.co.za>
2016-03-09 23:16:57vstinnerlinkissue26506 messages
2016-03-09 23:16:56vstinnercreate