[issue26118] String performance issue using single quotes

Eric V. Smith report at bugs.python.org
Thu Jan 14 19:29:00 EST 2016


Eric V. Smith added the comment:

I see a small difference, but I think it's related to the fact that in the first example you're concatenating 2 strings (',' and the result of {0}), and in the 2nd example it's 3 strings ("'", {0}, "',"):

$ echo '",{0}".format(x)'
",{0}".format(x)
$ python -m timeit -s 'x=4' '",{0}".format(x)'
1000000 loops, best of 3: 0.182 usec per loop

$ echo '"'\''{0}'\'',".format(x)'
"'{0}',".format(x)
$ python -m timeit -s 'x=4' '"'\''{0}'\'',".format(x)'
1000000 loops, best of 3: 0.205 usec per loop

If you see a factor of 30x difference in your code, I suspect it's not related to str.format(), but some other processing in your code.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26118>
_______________________________________


More information about the Python-bugs-list mailing list