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 wolma
Recipients docs@python, eric.smith, vstinner, wolma
Date 2016-03-09.21:15:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1457558146.35.0.234187004467.issue26506@psf.upfronthosting.co.za>
In-reply-to
Content
Your two suggestions prompted me to do a speed comparison between them and the result surprised me.

I tried:

import random
nums = [random.randint(0, 255) for n in range(10000000)]

then timed the simple:

for n in nums:
    hx = '%X' % n  # or hx = format(n, 'X')

I also tested a number of more complex formats like:
hx = '%{:02X}'.format(n) vs hx = '%%%02X' % n

In all cases, the old vs new formatting styles are rather similar in speed in my system Python 2.7.6 (with maybe a slight advantage for the format-based formatting).
In Python 3.5.0, however, old-style %-formatting is much speedier than under Python 2, while new-style formatting doesn't appear to have changed much, with the result that %-formatting is now between 30-50% faster than format-based formatting.

So I guess my questions are:

- are my timings wrong?

and if not:

- how got %-formatting improved (generally? or for %X specifically?)
- can this speed up be transferred to format-based formatting somehow?
History
Date User Action Args
2016-03-09 21:15:46wolmasetrecipients: + wolma, vstinner, eric.smith, docs@python
2016-03-09 21:15:46wolmasetmessageid: <1457558146.35.0.234187004467.issue26506@psf.upfronthosting.co.za>
2016-03-09 21:15:46wolmalinkissue26506 messages
2016-03-09 21:15:45wolmacreate