-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_and_plot.py
125 lines (104 loc) · 3.9 KB
/
time_and_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from matplotlib import ticker
import numpy as np
from timeit import timeit
from matplotlib import pyplot as plt
def time_and_plot(algo, Ns, dtype, func_names, time_func, xlabel, ylabel, yscale = 'linear', yscaling = 1):
"""Plot Timing Comparison
Timing Parameters
-----------------
algo : str
Name of the algorithm being timed. Example: "Matrix-Matrix Multiply",
Ns : indexable, int
Size of arguments to be passed to timed functions.
dtype : numpy.dtype
Type of arguments to pass to timed functions.
func_names : str
Name of the functions in the __main__ namespace to be timed.
time_func : function
Timing function. See time_func.
Plot Parameters
---------------
xlabel : str
See plt.xlabel.
ylabel : str
See plt.ylabel.
yscale : str, optional, defaults to 'linear'
See plt.set_yscale.
yscaling : integer, optional, defaults to 1
Ratio to multiply y data values by.
"""
data = np.empty((len(func_names), len(Ns)), dtype=np.float)
for k in xrange(len(func_names)):
for i in xrange(len(Ns)):
data[k,i] = time_func(func_names[k], Ns[i], dtype)
plt.clf()
fig1, ax1 = plt.subplots()
w, h = fig1.get_size_inches()
fig1.set_size_inches(w*1.5, h)
ax1.set_xscale('log')
ax1.get_xaxis().set_major_formatter(ticker.FormatStrFormatter('%d'))
ax1.get_xaxis().set_minor_locator(ticker.NullLocator())
# ax1.set_xticks(Ns)
ax1.set_yscale(yscale)
plt.setp(ax1.get_xticklabels(), fontsize=14)
plt.setp(ax1.get_yticklabels(), fontsize=14)
ax1.grid(color="lightgrey", linestyle="--", linewidth=1, alpha=0.5)
plt.xlabel(xlabel, fontsize=24)
plt.ylabel(ylabel, fontsize=24)
plt.xlim(Ns[0]*.9, Ns[-1]*1.1)
plt.suptitle("%s Performance" % (algo), fontsize=24)
for k in xrange(len(func_names)):
plt.plot(Ns, data[k,:]*yscaling, 'o-', linewidth=2, markersize=5, label=func_names[k])
plt.legend(loc='upper right', fontsize=18)
def time_sum_func(func_name, N=10000, dtype=np.int32, trials=100):
"""Timeit Helper Function (Simple One-D Functions)
Parameters
----------
func_name : str
Name of the function in the __main__ namespace to be timed.
N : int, optional, defaults to 10000
Size of np.ones array to construct and pass to function.
dtype : np.dtype
Type of array to construct and pass to function.
trials : int, optional, defaults to 100
This parameter is passed to timeit
Returns
-------
func_time : float
Average execution time over all trials
"""
import __main__
__main__.y = np.ones((N), dtype=dtype)
return (timeit(stmt="func(__main__.y)",
setup="import __main__; from __main__ import %s as func" % func_name,
number=trials)/trials)
def time_kernel(k_name, N, dtype):
"""Timeit Helper Function (GrowCut Functions)
Parameters
----------
k_name : str
Name of the GrowCut kernel in the __main__ namespace to be timed.
N : int
Size of image arrays to construct and pass to function.
dtype : np.dtype
Type of arrays to construct and pass to function.
Returns
-------
func_time : float
Average execution time over 3 trials
"""
import __main__
image = np.zeros((N, N, 3), dtype=dtype)
state = np.zeros((N, N, 2), dtype=dtype)
state_next = np.empty_like(state)
# colony 1 is strength 1 at position 0,0
# colony 0 is strength 0 at all other positions
state[0, 0, 0] = 1
state[0, 0, 1] = 1
__main__.image = image
__main__.state = state
__main__.state_next = state_next
trials = 3
return timeit(stmt="kernel(__main__.image, __main__.state, __main__.state_next, 10)",
setup="from __main__ import %s as kernel; import __main__" % k_name,
number=trials)/trials