@@ -58,8 +58,9 @@ then run the script with a special script (in this case with specific
58
58
arguments to the Python interpreter).
59
59
60
60
In the following example, we create a simple function ``my_func `` that
61
- allocates lists ``a ``, ``b `` and then deletes ``b ``::
61
+ allocates lists ``a ``, ``b `` and then deletes ``b ``:
62
62
63
+ .. code-block :: python
63
64
64
65
@profile
65
66
def my_func ():
@@ -101,7 +102,9 @@ the number of times that profiler has executed each line. The last column
101
102
102
103
Decorator
103
104
=========
104
- A function decorator is also available. Use as follows::
105
+ A function decorator is also available. Use as follows:
106
+
107
+ .. code-block :: python
105
108
106
109
from memory_profiler import profile
107
110
@@ -116,7 +119,9 @@ In this case the script can be run without specifying ``-m
116
119
memory_profiler `` in the command line.
117
120
118
121
In function decorator, you can specify the precision as an argument to the
119
- decorator function. Use as follows::
122
+ decorator function. Use as follows:
123
+
124
+ .. code-block :: python
120
125
121
126
from memory_profiler import profile
122
127
@@ -282,6 +287,7 @@ necessarily a Python program), a string containing some python code to
282
287
be evaluated or a tuple ``(f, args, kw) `` containing a function and its
283
288
arguments to be evaluated as ``f(*args, **kw) ``. For example,
284
289
290
+ .. code-block :: python
285
291
286
292
>> > from memory_profiler import memory_usage
287
293
>> > mem_usage = memory_usage(- 1 , interval = .2 , timeout = 1 )
@@ -301,8 +307,9 @@ thing on the IPython notebook it scales up to 44MB.
301
307
302
308
If you'd like to get the memory consumption of a Python function, then
303
309
you should specify the function and its arguments in the tuple ``(f,
304
- args, kw) ``. For example::
310
+ args, kw) ``. For example:
305
311
312
+ .. code-block :: python
306
313
307
314
>> > # define a simple function
308
315
>> > def f (a , n = 100 ):
@@ -325,6 +332,8 @@ REPORTING
325
332
The output can be redirected to a log file by passing IO stream as
326
333
parameter to the decorator like @profile(stream=fp)
327
334
335
+ .. code-block :: python
336
+
328
337
>> > fp= open (' memory_profiler.log' ,' w+' )
329
338
>> > @ profile(stream = fp)
330
339
>> > def my_func ():
@@ -333,7 +342,7 @@ parameter to the decorator like @profile(stream=fp)
333
342
... del b
334
343
... return a
335
344
336
- For details refer: examples/reporting_file.py
345
+ For details refer: examples/reporting_file.py
337
346
338
347
``Reporting via logger Module: ``
339
348
@@ -343,6 +352,8 @@ when we need to use RotatingFileHandler.
343
352
The output can be redirected to logger module by simply making use of
344
353
LogFile of memory profiler module.
345
354
355
+ .. code-block :: python
356
+
346
357
>> > from memory_profiler import LogFile
347
358
>> > import sys
348
359
>> > sys.stdout = LogFile(' memory_profile_log' )
@@ -354,11 +365,13 @@ could be cumbersome and one can choose only entries with increments
354
365
by passing True to reportIncrementFlag, where reportIncrementFlag is
355
366
a parameter to LogFile class of memory profiler module.
356
367
368
+ .. code-block :: python
369
+
357
370
>> > from memory_profiler import LogFile
358
371
>> > import sys
359
372
>> > sys.stdout = LogFile(' memory_profile_log' , reportIncrementFlag = False )
360
373
361
- For details refer: examples/reporting_logger.py
374
+ For details refer: examples/reporting_logger.py
362
375
363
376
=====================
364
377
IPython integration
@@ -372,7 +385,9 @@ For IPython 0.11+, you can use the module directly as an extension, with
372
385
To activate it whenever you start IPython, edit the configuration file for your
373
386
IPython profile, ~/.ipython/profile_default/ipython_config.py, to register the
374
387
extension like this (If you already have other extensions, just add this one to
375
- the list)::
388
+ the list):
389
+
390
+ .. code-block :: python
376
391
377
392
c.InteractiveShellApp.extensions = [
378
393
' memory_profiler' ,
@@ -385,28 +400,36 @@ It then can be used directly from IPython to obtain a line-by-line
385
400
report using the `%mprun ` or `%%mprun ` magic command. In this case, you can skip
386
401
the `@profile ` decorator and instead use the `-f ` parameter, like
387
402
this. Note however that function my_func must be defined in a file
388
- (cannot have been defined interactively in the Python interpreter)::
403
+ (cannot have been defined interactively in the Python interpreter):
404
+
405
+ .. code-block :: python
389
406
390
407
In [1 ]: from example import my_func, my_func_2
391
408
392
409
In [2 ]: % mprun - f my_func my_func()
393
410
394
- or in cell mode::
411
+ or in cell mode:
412
+
413
+ .. code-block :: python
395
414
396
415
In [3 ]: %% mprun - f my_func - f my_func_2
397
416
... : my_func()
398
417
... : my_func_2()
399
418
400
419
Another useful magic that we define is `%memit `, which is analogous to
401
- `%timeit `. It can be used as follows::
420
+ `%timeit `. It can be used as follows:
421
+
422
+ .. code-block :: python
402
423
403
424
In [1 ]: % memit range (10000 )
404
425
peak memory: 21.42 MiB, increment: 0.41 MiB
405
426
406
427
In [2 ]: % memit range (1000000 )
407
428
peak memory: 52.10 MiB, increment: 31.08 MiB
408
429
409
- or in cell mode (with setup code)::
430
+ or in cell mode (with setup code):
431
+
432
+ .. code-block :: python
410
433
411
434
In [3 ]: %% memit l= range (1000000 )
412
435
... : len (l)
@@ -416,7 +439,9 @@ or in cell mode (with setup code)::
416
439
For more details, see the docstrings of the magics.
417
440
418
441
For IPython 0.10, you can install it by editing the IPython configuration
419
- file ~/.ipython/ipy_user_conf.py to add the following lines::
442
+ file ~/.ipython/ipy_user_conf.py to add the following lines:
443
+
444
+ .. code-block :: python
420
445
421
446
# These two lines are standard and probably already there.
422
447
import IPython.ipapi
@@ -441,6 +466,8 @@ Currently, the backend can be set via the CLI
441
466
442
467
and is exposed by the API
443
468
469
+ .. code-block :: python
470
+
444
471
>> > from memory_profiler import memory_usage
445
472
>> > mem_usage = memory_usage(- 1 , interval = .2 , timeout = 1 , backend = " psutil" )
446
473
0 commit comments