Message261481
> 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. |
|
Date |
User |
Action |
Args |
2016-03-09 23:16:57 | vstinner | set | recipients:
+ vstinner, eric.smith, docs@python, wolma |
2016-03-09 23:16:57 | vstinner | set | messageid: <1457565417.41.0.0556401980322.issue26506@psf.upfronthosting.co.za> |
2016-03-09 23:16:57 | vstinner | link | issue26506 messages |
2016-03-09 23:16:56 | vstinner | create | |
|