Message266016
I benchmarked some f'' strings against .format, and surprisingly f'' was slower than .format in about all the simple cases I could think of. I guess this is because f'' strings implicitly use `''.join([])`.
The byte code for f'foo is {foo}' currently is
1 0 LOAD_CONST 1 ('')
3 LOAD_ATTR 0 (join)
6 LOAD_CONST 2 ('foo is ')
9 LOAD_GLOBAL 1 (foo)
12 FORMAT_VALUE 0
15 BUILD_LIST 2
18 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
It was mentioned here https://bugs.python.org/issue24965 but since I came up with the idea when inspecting this, I'd propose again here that a new opcode be added for f'' strings - BUILD_STRING n, with which f'foo is {foo}' could be compiled to
0 LOAD_CONST 2 ('foo is ')
3 LOAD_GLOBAL 1 (foo)
6 FORMAT_VALUE 0
9 BUILD_STRING 2
instead. Internally this wouldn't even need to call `PyUnicode_Join`, but instead `seplen == 0` case could be refactored into a separate function. Not only with this is it possible to know the length of the string, but also the number of string components are already known, so it'd be possible to build a tuple fast from the values on stack.
And that way people doing micro benchmarks would find out that the new Python 3.6 feature would not only look nice but also be a way to write better-performing code. |
|
Date |
User |
Action |
Args |
2016-05-21 19:18:36 | ztane | set | recipients:
+ ztane |
2016-05-21 19:18:36 | ztane | set | messageid: <1463858316.54.0.292581402272.issue27078@psf.upfronthosting.co.za> |
2016-05-21 19:18:36 | ztane | link | issue27078 messages |
2016-05-21 19:18:36 | ztane | create | |
|