-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed as not planned
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
After a long, semi-happy benchmark weekend (darn those non-deterministic instruction counts, even without GC and PYTHONHASHSEED set and exclusive core pinning...) I just stumbled onto an issue with f-string performance under timeit runtime when assigning to one of the joined variables.
Tested with Python 3.11.9
and 3.12.4
, Linux (Manjaro OS). Also tested online on various python run time sites to rule out my OS as the culprit.
import timeit
print("String concatenations")
print(timeit.timeit("string3 = f'{string}{string2}'", "string='string'; string2='string2'", number=100_000), "string3 = f'{string}{string2}'")
print(timeit.timeit("string2 = f'{string}{string2}'", "string='string'; string2='string2'", number=100_000), "string2 = f'{string}{string2}'")
print(timeit.timeit("string = f'{string}{string2}'", "string='string'; string2='string2'", number=100_000), "string = f'{string}{string2}'")
String concatenations
0.005295750990626402 string3 = f'{string}{string2}'
6.830826308010728 string2 = f'{string}{string2}'
4.56450519200007 string = f'{string}{string2}'
The slowdown is exponential.
import timeit
print("String concatenations")
print(timeit.timeit("string3 = f'{string}{string2}'", "string='string'; string2='string2'", number=200_000), "string3 = f'{string}{string2}'")
print(timeit.timeit("string2 = f'{string}{string2}'", "string='string'; string2='string2'", number=200_000), "string2 = f'{string}{string2}'")
print(timeit.timeit("string = f'{string}{string2}'", "string='string'; string2='string2'", number=200_000), "string = f'{string}{string2}'")
String concatenations
0.01072984401253052 string3 = f'{string}{string2}'
29.431256133000716 string2 = f'{string}{string2}'
16.437050897002337 string = f'{string}{string2}'
I couldn't replicate this slowdown using python -m timeit
or with simple f-string assignment scripts. My guess is an issue with timeit, but maybe this a weird edge case for f-strings?
CPython versions tested on:
3.11, 3.12
Operating systems tested on:
Linux
fruitoiz
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error