Stupid Python tricks

Steven D'Aprano steve at pearwood.info
Wed Dec 30 22:51:22 EST 2015


Stolen^W Inspired from a post by Tim Peters back in 2001:

https://mail.python.org/pipermail/python-dev/2001-January/011911.html


Suppose you have a huge string, and you want to quote it. Here's the obvious
way:

mystring = "spam"*100000
result = '"' + mystring + '"'


But that potentially involves a lot of copying. How fast is it? Using
Jython2.5, I get these results on my computer:

jy> from timeit import Timer
jy> t = Timer("""'"' + mystring + '"'""", 'mystring = "spam"*100000')
jy> min(t.repeat(number=1000))
2.4110000133514404



Perhaps % interpolation is faster?

jy> t = Timer("""'"%s"' % mystring""", 'mystring = "spam"*100000')
jy> min(t.repeat(number=1000))
2.9660000801086426



Ouch, that's actually worse. But now we have the Stupid Python Trick:
result = mystring.join('""')

How fast is this?

jy> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
jy> min(t.repeat(number=1000))
2.171999931335449


That's Jython, which is not known for its speed. (If you want speed in
Jython, you ought to be calling Java libraries.) Here are some results
using Python 3.3:

py> from timeit import Timer
py> t = Timer("""'"' + mystring + '"'""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.22504080459475517



Using % interpolation and the format method:

py> t = Timer("""'"{}"'.format(mystring)""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.4634905573911965
py> t = Timer("""'"%s"' % mystring""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.474040764849633



And the Stupid Python Trick:

py> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.19407050590962172


Fifteen years later, and Tim Peters' Stupid Python Trick is still the
undisputed champion!


-- 
Steven




More information about the Python-list mailing list