Within the Python docs essays section is a wonderful article by Guido van Rossum on optimizing code with Python. You should read the whole thing but I’m highlighting the bits I found most interesting and include a Python 3 version of the working code at the end.

The specific computer science problem he tries to solve is, How do you efficiently convert a list of integers that are ASCII values into a string.

It’s fascinating to see even Guido himself reason through the problem in normal fashion, starting with a simple solution and then trying out various optimizations.

His first take is simple and elegant:

``````def f1(list):
string = ""
for item in list:
string = string + chr(item)
return string

>>> int_list = [97, 98, 99]
>>> f1(int_list)
'abc'
``````

And by version 7 he’s way, way faster. The progression and the way he tests the time execution of each is particularly interesting.

Here’s the complete Python 3 code (his example is older and uses Python 2):

``````import time
import functools
import array

def timing(f, n, a):
print(f.__name__,)
r = range(n)
t1 = time.clock()
for i in r:
f(a)
f(a)
f(a)
f(a)
f(a)
f(a)
f(a)
f(a)
f(a)
f(a)
t2 = time.clock()
print(round(t2 - t1, 3))

def f1(list):
string = ""
for item in list:
string = string + chr(item)
return string

def f2(list):
return functools.reduce(lambda string, item: string + chr(item), list, "")

def f3(list):
string = ""
for character in map(chr, list):
string = string + character
return string

def f4(list):
string = ''
lchr = chr
for item in list:
string = string + lchr(item)
return string

def f5(list):
string = ''
for i in range(0, 256, 16):  # 0, 16, 32...
s = ''
for character in map(chr, list[i:i + 16]):
s = s + character
string = string + s
return string

def f6(list):
return ''.join(map(chr, list))

def f7(list):
return array.array('B', list).tostring()

testdata = list(range(256))
print(testdata)
testfuncs = f1, f2, f3, f4, f5, f6, f7
for f in testfuncs:
print(f.__name__, f(testdata))
for f in testfuncs:
timing(f, 100, testdata)
``````

Want to improve your Python? I have a list of recommended Python books.