Why are f-strings faster than str() to parse values?
问题 I was playing around with f-strings, (see PEP 498), and I decided to check the speed of the f-string parse, (e.g. f"{1}") in comparison with the usual str parse (e.g str(1)). But for my surprise, when I checked the velocity of both methods with the timeit function, I found out that >>> from timeit import timeit >>> timeit("f'{1}'") 0.1678762999999961 whereas >>> timeit("str(1)") 0.3216999999999999 or even the repr func, which in most of the cases is faster than str cast >>> timeit("repr(1)")