3 ms·
It would seem that loop bookkeeping dominates execution time here. Without it, I get quite different results: >>> timeit.timeit("'%s' % i", setup="i = 42",
by Xion 14y ago
It would seem that loop bookkeeping dominates execution time here. Without it, I get quite different results:
>>> timeit.timeit("'%s' % i", setup="i = 42", number=1000000)
0.31799793243408203
>>> timeit.timeit("str(i)", setup="i = 42", number=1000000)
0.4146881103515625
This is another argument for profiling everything.
- jedbrown 14y agoOn the contrary, the time per inner iteration is slightly higher this way and the same conclusions hold: 1. python3.2 str() is at least as fast as '%s' formatting 2. '%s' is slightly faster than str() with python2.7 3. theint.__str__() is faster than either alternative in all cases
- zurn 14y agoAnd also, for using timeit correctly. The command line usage is handy too: python -m timeit -s "i=42" "str(i)"