You can use the timeit
package as shown in this blog post.
Here is a complete code running the tests 20000 times each test:
import timeit
t = 20000
print( "Addition (lst = lst + [4, 5, 6])" )
print( timeit.Timer("lst = lst + [4, 5, 6]", "lst = [1, 2, 3]").timeit(t) )
print( "Addition (lst += [4, 5, 6])" )
print( timeit.Timer("lst += [4, 5, 6]", "lst = [1, 2, 3]").timeit(t) )
print( "Extend (lst.extend([4, 5, 6]))" )
print( timeit.Timer("lst.extend([4, 5, 6])", "lst = [1, 2, 3]").timeit(t) )
print( "Append loop (lst.append([4, 5, 6]))" )
print( timeit.Timer("for i in [4,5,6]: lst.append(i)", "lst = [1,2,3]").timeit(t) )
print( "Append loop, no dot (a(i))" )
# a.b does a lookup, we don't want that, it is slower. Instead use b = a.b
# then use b.
print( timeit.Timer("""a = lst.append
for i in [4,5,6]: a(i)""", "lst = [1,2,3]").timeit(t) )
And the results (Python 3.4.4) are:
Addition (lst = lst + [4, 5, 6])
1.947201736000352
Addition (lst += [4, 5, 6])
0.0015889199999037373
Extend (lst.extend([4, 5, 6]))
0.0020685689996753354
Append loop (lst.append([4, 5, 6]))
0.0047527769997941505
Append loop, no dot (a(i))
0.003853704999983165