Added profiling
[advent-of-code-21.git] / profiling / profiling.md
1 ---
2 jupyter:
3 jupytext:
4 formats: ipynb,md
5 text_representation:
6 extension: .md
7 format_name: markdown
8 format_version: '1.3'
9 jupytext_version: 1.11.1
10 kernelspec:
11 display_name: Python 3 (ipykernel)
12 language: python
13 name: python3
14 ---
15
16 ```python Collapsed="false"
17 import glob
18 import json
19 import pandas as pd
20 import numpy as np
21
22 import matplotlib.pyplot as plt
23 %matplotlib inline
24 ```
25
26 ```python Collapsed="false" tags=[]
27 ! cd .. && for i in {01..25}; do cabal run advent${i} --enable-profiling -- +RTS -N -pj -s -hT ; done
28 ```
29
30 ```python
31 ! rm ../times.csv
32 ! rm ../times_raw.csv
33 ```
34
35 ```python Collapsed="false" tags=[]
36 ! cd .. && for i in {01..25}; do /usr/bin/time -f "%C,%S,%E,%M" -o times.csv -a cabal run advent${i}; done
37 ```
38
39 ```python Collapsed="false" tags=[]
40 ! cd .. && for i in {01..25}; do /usr/bin/time -f "%C,%S,%E,%M" -o times_raw.csv -a advent${i}; done
41 ```
42
43 ```python
44 !mv ../*prof .
45 ```
46
47 ```python
48 !mv ../times.csv .
49 ```
50
51 ```python
52 !mv ../times_raw.csv .
53 ```
54
55 ```python
56 !mv ../*hp .
57 ```
58
59 ```python
60 ! for f in *hp ; do hp2ps ${f} ; done
61 ```
62
63 ```python Collapsed="false"
64 len(glob.glob('*prof'))
65 ```
66
67 ```python Collapsed="false"
68 profs = []
69 for fn in glob.glob('*prof'):
70 with open(fn) as f:
71 j = json.load(f)
72 prof = {}
73 for n in 'program total_time total_alloc total_ticks initial_capabilities'.split():
74 prof[n] = j[n]
75 profs.append(prof)
76 profs
77 ```
78
79 ```python Collapsed="false"
80 performance = pd.DataFrame(profs).set_index('program').sort_index()
81 performance
82 ```
83
84 ```python Collapsed="false"
85 performance.total_ticks.plot.bar()
86 ```
87
88 ```python Collapsed="false"
89 performance.total_ticks.plot.bar(logy=True)
90 ```
91
92 ```python Collapsed="false"
93 performance.total_alloc.plot.bar()
94 ```
95
96 ```python Collapsed="false"
97 performance.total_alloc.plot.bar(logy=True)
98 ```
99
100 ```python Collapsed="false"
101 performance[['total_ticks', 'total_alloc']].plot.bar(
102 logy=True, secondary_y=['total_alloc'],
103 figsize=(8, 6), title="Internal time and memory")
104 plt.savefig('internal_time_and_memory_log.png')
105 ```
106
107 ```python Collapsed="false"
108 performance[['total_ticks', 'total_alloc']].plot.bar(
109 logy=False, secondary_y=['total_alloc'],
110 figsize=(8, 6), title="Internal time and memory")
111 plt.savefig('internal_time_and_memory_linear.png')
112 ```
113
114 ```python
115 # times = pd.read_csv('times.csv',
116 # names=['program', 'system', 'elapsed', 'memory'],
117 # index_col='program')
118 # times.index = times.index.str.slice(start=len('cabal run '))
119 # times.elapsed = pd.to_numeric(times.elapsed.str.slice(start=2))
120 # times
121 ```
122
123 ```python
124 times = pd.read_csv('times_raw.csv',
125 names=['program', 'system', 'elapsed', 'memory'],
126 index_col='program')
127 times.elapsed = pd.to_numeric(times.elapsed.str.slice(start=2))
128 times
129 ```
130
131 ```python
132 times.dtypes
133 ```
134
135 ```python Collapsed="false"
136 times.describe()
137 ```
138
139 ```python Collapsed="false"
140 performance = performance.merge(times, left_index=True, right_index=True)
141 # performance.drop(index='advent15loop', inplace=True)
142 performance
143 ```
144
145 ```python Collapsed="false"
146 performance.columns
147 ```
148
149 ```python Collapsed="false"
150 # performance[['total_ticks', 'elapsed']].plot.bar(logy=True)
151 performance[['elapsed', 'memory']].plot.bar(
152 logy=True, secondary_y=['memory'],
153 figsize=(8, 6), title="External time and memory")
154 plt.savefig('external_time_and_memory_log.png')
155 ```
156
157 ```python Collapsed="false"
158 # performance[['total_ticks', 'elapsed']].plot.bar(logy=True)
159 performance[['elapsed', 'memory']].plot.bar(
160 logy=False, secondary_y=['memory'],
161 figsize=(8, 6), title="External time and memory")
162 plt.savefig('external_time_and_memory_linear.png')
163 ```
164
165 ```python Collapsed="false"
166 # performance[['total_ticks', 'elapsed']].plot.bar(logy=True)
167 performance[['total_ticks', 'elapsed']].plot.bar(
168 logy=True, secondary_y=['elapsed'],
169 figsize=(8, 6), title="Internal vs external time")
170 plt.savefig('internal_external_time.png')
171 ```
172
173 ```python Collapsed="false"
174 # performance[['total_ticks', 'elapsed']].plot.bar(logy=True)
175 performance[['total_ticks', 'elapsed']].plot.bar(
176 logy=False, secondary_y=['elapsed'],
177 figsize=(8, 6), title="Internal vs external time")
178 plt.savefig('internal_external_time_linear.png')
179 ```
180
181 ```python Collapsed="false"
182 # performance[['total_ticks', 'elapsed']].plot.bar(logy=True)
183 performance[['total_alloc', 'memory']].plot.bar(
184 logy=True, secondary_y=['memory'],
185 figsize=(8, 6), title="Internal vs external memory")
186 plt.savefig('internal_external_memory_log.png')
187 ```
188
189 ```python Collapsed="false"
190 # performance[['total_ticks', 'elapsed']].plot.bar(logy=True)
191 performance[['total_alloc', 'memory']].plot.bar(
192 logy=False, secondary_y=['memory'],
193 figsize=(8, 6), title="Internal vs external memory")
194 plt.savefig('internal_external_memory_linear.png')
195 ```
196
197 ```python Collapsed="false"
198 # performance['elapsed_adj'] = performance['elapsed'] - 0.28
199 # performance
200 ```
201
202 ```python Collapsed="false"
203 # performance[['total_time', 'elapsed_adj']].plot.bar(logy=True)
204 ```
205
206 ```python Collapsed="false"
207 fig, ax = plt.subplots(ncols=3, figsize=(20,5))
208
209 performance['elapsed'].plot.bar(ax=ax[2],
210 logy=True,
211 title="Run times (wall clock), log scale",
212 # figsize=(10,8)
213 )
214 ax[2].set_xlabel('Program')
215
216 performance['elapsed'].plot.bar(ax=ax[0],
217 logy=False,
218 title="Run times (wall clock), linear scale",
219 # figsize=(10,8)
220 )
221 ax[0].set_xlabel('Program')
222
223 performance['elapsed'].plot.bar(ax=ax[1],
224 logy=False,
225 ylim=(0, 5.2),
226 title="Run times (wall clock), truncated linear scale",
227 # figsize=(10,8)
228 )
229 ax[1].set_xlabel('Program')
230
231 plt.savefig('run_times_combined.png')
232 ```
233
234 ```python Collapsed="false"
235 fig, ax = plt.subplots(ncols=2, figsize=(13,5))
236
237 performance['memory'].plot.bar(ax=ax[0],
238 logy=True,
239 title="Memory used, log scale",
240 # figsize=(10,8)
241 )
242 ax[0].set_xlabel('Program')
243
244 performance['memory'].plot.bar(ax=ax[1],
245 logy=False,
246 title="Memory used, linear scale",
247 # figsize=(10,8)
248 )
249 ax[1].set_xlabel('Program')
250
251 plt.savefig('memory_combined.png')
252 ```
253
254 ```python Collapsed="false"
255 # ax = performance['elapsed_adj'].plot.bar(logy=False,
256 # title="Run times (wall clock), linear scale",
257 # figsize=(10,8))
258 # ax.set_xlabel('Program')
259 # plt.savefig('run_times_linear.png')
260 ```
261
262 ```python Collapsed="false"
263 performance.columns
264 ```
265
266 ```python Collapsed="false"
267 performance['memory'].plot.bar()
268 ```
269
270 ```python Collapsed="false"
271 performance.plot.scatter('elapsed', 'total_alloc', logx=True, logy=True)
272 ```
273
274 ```python Collapsed="false"
275 performance.plot.scatter('memory', 'total_alloc', logx=True, logy=True)
276 ```
277
278 ```python Collapsed="false"
279 performance.plot.scatter('elapsed', 'total_ticks', logx=True, logy=True)
280 ```
281
282 ```python Collapsed="false"
283 performance[['total_alloc', 'memory', 'elapsed']].to_csv('performance.csv')
284 ```
285
286 ```python Collapsed="false"
287 print(performance[['total_alloc', 'elapsed', 'memory']].to_markdown(floatfmt=['0.0f', '0.0f', '.2f', '0.0f']))
288 ```
289
290 ```python
291
292 ```