So I have been playing around with f-strings and their speeds in comparison in different scenarios. I ran into a scenario where f strings are slower.
Edit: x = 0
In[1]: %timeit f"{x:0128x}"
363 ns ± 1.69 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[2]: %timeit '%0128x' % x
224 ns ± 1.37 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[3]: %timeit f"{x:0128X}"
533 ns ± 22 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[4]: %timeit "%0128X" % x
222 ns ± 0.408 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Why are f-strings slower in this scenario, and why is 'X' so much slower than 'x' for f-strings?
xis an integer.f-strings) is a much more complex operation than the other.% xusesxas a local,f"..."may well be looking upxas a global. Put both in a function to ensure equal scopes. I then get 430ns and 304ns per loop.