@@ -6,8 +6,7 @@ and profile Python code in various forms.
6
6
7
7
For the following, we assume that we have:
8
8
9
- * the below file ``fib.py `` in the current directory,
10
- * the current directory in ``${PYTHONPATH} ``, and
9
+ * the below file ``fib.py `` in the current directory, and
11
10
* :py:mod: `line_profiler ` and :py:mod: `kernprof ` installed.
12
11
13
12
.. code :: python
@@ -62,10 +61,18 @@ Script execution
62
61
In the most basic form, one passes the path to the executed script and
63
62
its arguments to ``kernprof ``:
64
63
65
- .. code :: console
64
+ .. code :: bash
65
+
66
+ kernprof --prof-mod fib.py --line-by-line --view \
67
+ fib.py --verbose 10 20 30
68
+
69
+ .. raw :: html
70
+
71
+ <details >
72
+ <summary >Output (click to expand)</summary >
73
+
74
+ .. code ::
66
75
67
- $ kernprof --prof-mod fib.py --line-by-line --view \
68
- > fib.py --verbose 10 20 30
69
76
fib(10) = 89
70
77
fib(20) = 10946
71
78
fib(30) = 1346269
@@ -126,6 +133,12 @@ its arguments to ``kernprof``:
126
133
37 3 91.0 30.3 18.7 result = func(n)
127
134
38 3 20.0 6.7 4.1 print(pattern.format(n, result))
128
135
136
+ .. raw :: html
137
+
138
+ </details >
139
+ <p >
140
+
141
+
129
142
.. _kernprof-script-note :
130
143
.. note ::
131
144
@@ -141,16 +154,30 @@ Module execution
141
154
It is also possible to use ``kernprof -m `` to run installed modules and
142
155
packages:
143
156
144
- .. code :: console
157
+ .. code :: bash
158
+
159
+ PYTHONPATH=" ${PYTHONPATH} :${PWD} " \
160
+ kernprof --prof-mod fib --line-by-line --view -m \
161
+ fib --verbose 10 20 30
162
+
163
+ .. raw :: html
164
+
165
+ <details >
166
+ <summary >Output (click to expand)</summary >
167
+
168
+ .. code ::
145
169
146
- $ kernprof --prof-mod fib --line-by-line --view -m \
147
- > fib --verbose 10 20 30
148
170
fib(10) = 89
149
171
fib(20) = 10946
150
172
fib(30) = 1346269
151
173
Wrote profile results to fib.lprof
152
174
...
153
175
176
+ .. raw :: html
177
+
178
+ </details >
179
+ <p >
180
+
154
181
.. _kernprof-m-note :
155
182
.. note ::
156
183
@@ -159,13 +186,25 @@ packages:
159
186
thereafter (the run module).
160
187
If there isn't one, an error is raised:
161
188
162
- .. code :: console
189
+ .. code :: bash
190
+
191
+ kernprof -m
192
+
193
+ .. raw :: html
194
+
195
+ <details >
196
+ <summary >Output (click to expand)</summary >
197
+
198
+ .. code :: pycon
163
199
164
- $ kernprof -m
165
200
Traceback (most recent call last):
166
201
...
167
202
ValueError: argument expected for the -m option
168
203
204
+ .. raw :: html
205
+
206
+ </details >
207
+
169
208
170
209
Literal-code execution
171
210
----------------------
@@ -174,12 +213,23 @@ Like how ``kernprof -m`` parallels ``python -m``, ``kernprof -c`` can be
174
213
used to run and profile literal snippets supplied on the command line
175
214
like ``python -c ``:
176
215
177
- .. code :: console
216
+ .. code :: bash
217
+
218
+ PYTHONPATH=" ${PYTHONPATH} :${PWD} " \
219
+ kernprof --prof-mod fib._run_fib --line-by-line --view -c "
220
+ import sys
221
+ from fib import _run_fib, fib_no_cache as fib
222
+ for n in sys.argv[1:]:
223
+ print(f'fib({n})', '=', fib(int(n)))
224
+ " 10 20
225
+
226
+ .. raw :: html
227
+
228
+ <details >
229
+ <summary >Output (click to expand)</summary >
230
+
231
+ .. code ::
178
232
179
- $ code="import sys; "
180
- $ code+="from fib import _run_fib, fib_no_cache as fib; "
181
- $ code+="for n in sys.argv[1:]: print(f'fib({n})', '=', fib(int(n)))"
182
- $ kernprof --prof-mod fib._run_fib --line-by-line --view -c "${code}" 10 20
183
233
fib(10) = 89
184
234
fib(20) = 10946
185
235
Wrote profile results to <...>/kernprof-command-imuhz89_.lprof
@@ -200,6 +250,11 @@ like ``python -c``:
200
250
22 11033 1477.0 0.1 18.4 prev = fib(n - 1)
201
251
23 11033 770.0 0.1 9.6 return prev_prev + prev
202
252
253
+ .. raw :: html
254
+
255
+ </details >
256
+ <p >
257
+
203
258
.. note ::
204
259
205
260
* As with ``python -c ``, the ``-c `` option terminates further
@@ -215,18 +270,25 @@ like ``python -c``:
215
270
``python -m line_profiler `` and has to be ``--view ``-ed
216
271
immediately:
217
272
218
- .. code :: console
219
-
220
- $ read -d '' -r code <<-'!'
221
- > from fib import fib
222
- >
223
- > def my_func(n=50):
224
- > result = fib(n)
225
- > print(n, '->', result)
226
- >
227
- > my_func()
228
- > !
229
- $ kernprof -lv -c "${code}"
273
+ .. code :: bash
274
+
275
+ PYTHONPATH=" ${PYTHONPATH} :${PWD} " \
276
+ kernprof --line-by-line --view -c "
277
+ from fib import fib
278
+
279
+ def my_func(n=50):
280
+ result = fib(n)
281
+ print(n, '->', result)
282
+
283
+ my_func()"
284
+
285
+ .. raw :: html
286
+
287
+ <details >
288
+ <summary >Output (click to expand)</summary >
289
+
290
+ .. code ::
291
+
230
292
50 -> 20365011074
231
293
Wrote profile results to <...>/kernprof-command-ni6nis6t.lprof
232
294
Timer unit: 1e-06 s
@@ -241,7 +303,22 @@ like ``python -c``:
241
303
4 1 26.0 26.0 68.4 result = fib(n)
242
304
5 1 12.0 12.0 31.6 print(n, '->', result)
243
305
244
- $ python -m line_profiler kernprof-command-ni6nis6t.lprof
306
+ .. raw :: html
307
+
308
+ </details >
309
+ <p >
310
+
311
+ .. code :: bash
312
+
313
+ python -m line_profiler kernprof-command-ni6nis6t.lprof
314
+
315
+ .. raw :: html
316
+
317
+ <details >
318
+ <summary >Output (click to expand)</summary >
319
+
320
+ .. code ::
321
+
245
322
Timer unit: 1e-06 s
246
323
247
324
Total time: 3.6e-05 s
@@ -257,26 +334,46 @@ like ``python -c``:
257
334
4 1 26.0 26.0 72.2
258
335
5 1 10.0 10.0 27.8
259
336
337
+ .. raw :: html
338
+
339
+ </details >
340
+
260
341
261
342
Executing code read from ``stdin ``
262
343
----------------------------------
263
344
264
345
It is also possible to read, run, and profile code from ``stdin ``, by
265
346
passing ``- `` to ``kernprof `` in place of a filename:
266
347
267
- .. code :: console
348
+ .. code :: bash
349
+
350
+ {
351
+ # This example doesn't make much sense on its own, but just
352
+ # imagine if this is a command generating code dynamically
353
+ echo ' import sys'
354
+ echo ' from fib import _run_fib, fib_no_cache as fib'
355
+ echo ' for n in sys.argv[1:]:'
356
+ echo ' print(f"fib({n})", "=", fib(int(n)))'
357
+ } | PYTHONPATH=" ${PYTHONPATH} :${PWD} " \
358
+ kernprof --prof-mod fib._run_fib --line-by-line --view - 10 20
359
+
360
+ .. raw :: html
361
+
362
+ <details >
363
+ <summary >Output (click to expand)</summary >
364
+
365
+ .. code ::
268
366
269
- $ kernprof --prof-mod fib._run_fib --line-by-line --view - 10 20 <<-'!'
270
- > import sys
271
- > from fib import _run_fib, fib_no_cache as fib
272
- > for n in sys.argv[1:]:
273
- > print(f"fib({n})", "=", fib(int(n)))
274
- > !
275
367
fib(10) = 89
276
368
fib(20) = 10946
277
369
Wrote profile results to <...>/kernprof-stdin-kntk2lo1.lprof
278
370
...
279
371
372
+ .. raw :: html
373
+
374
+ </details >
375
+ <p >
376
+
280
377
.. note ::
281
378
282
379
Since the temporary file containing the executed code will not exist
0 commit comments