plotting performance

Go Back home

Sometimes you want to do some quick performance testing in python. This is very easy to do with the perfplot module.

 1 import functools
 2 import itertools
 3 import numpy
 4 import operator
 5 import perfplot
 6 
 7 def forfor(a):
 8     return [item for sublist in a for item in sublist]
 9 
10 def sum_brackets(a):
11     return sum(a, [])
12  
13 def functools_reduce(a):
14     return functools.reduce(operator.concat, a)
15 
16 
17 def functools_reduce_iconcat(a):
18     return functools.reduce(operator.iconcat, a, [])
19 
20 
21 def itertools_chain(a):
22     return list(itertools.chain.from_iterable(a))
23 
24 
25 def numpy_flat(a):
26     return list(numpy.array(a).flat)
27 
28 
29 def numpy_concatenate(a):
30     return list(numpy.concatenate(a))
31 
32 
33 perfplot.show(
34     setup=lambda n: [list(range(10))] * n,
35     # setup=lambda n: [list(range(n))] * 10,
36     kernels=[
37         forfor,
38         sum_brackets,
39         functools_reduce,
40         functools_reduce_iconcat,
41         itertools_chain,
42         numpy_flat,
43         numpy_concatenate,
44     ],
45     n_range=[2 ** k for k in range(16)],
46     xlabel="num lists (of length 10)",
47     # xlabel="len lists (10 lists total)"
48 )