@@ -49,6 +49,30 @@ def unicode(x, *args): return str(x)
4949 has_psutil = False
5050
5151
52+ class MemitResult (object ):
53+ """memit magic run details.
54+
55+ Object based on IPython's TimeitResult
56+ """
57+ def __init__ (self , mem_usage , baseline , repeat , timeout , interval ,
58+ include_children ):
59+ self .mem_usage = mem_usage
60+ self .baseline = baseline
61+ self .repeat = repeat
62+ self .timeout = timeout
63+ self .interval = interval
64+ self .include_children = include_children
65+
66+ def __str__ (self ):
67+ max_mem = max (self .mem_usage )
68+ inc = max_mem - self .baseline
69+ return 'peak memory: %.02f MiB, increment: %.02f MiB' % (max_mem , inc )
70+
71+ def _repr_pretty_ (self , p , cycle ):
72+ msg = str (self )
73+ p .text (u'<MemitResult : ' + msg + u'>' )
74+
75+
5276def _get_memory (pid , timestamps = False , include_children = False ):
5377
5478 # .. only for current process and only on unix..
@@ -794,6 +818,10 @@ def memit(self, line='', cell=None):
794818
795819 -c: If present, add the memory usage of any children process to the report.
796820
821+ -o: If present, return a object containing memit run details
822+
823+ -q: If present, be quiet and do not output a result.
824+
797825 Examples
798826 --------
799827 ::
@@ -811,7 +839,7 @@ def memit(self, line='', cell=None):
811839
812840 """
813841 from memory_profiler import memory_usage , _func_exec
814- opts , stmt = self .parse_options (line , 'r:t:i:c ' , posix = False , strict = False )
842+ opts , stmt = self .parse_options (line , 'r:t:i:coq ' , posix = False , strict = False )
815843
816844 if cell is None :
817845 setup = 'pass'
@@ -827,6 +855,8 @@ def memit(self, line='', cell=None):
827855 timeout = None
828856 interval = float (getattr (opts , 'i' , 0.1 ))
829857 include_children = 'c' in opts
858+ return_result = 'o' in opts
859+ quiet = 'q' in opts
830860
831861 # I've noticed we get less noisier measurements if we run
832862 # a garbage collection first
@@ -835,22 +865,28 @@ def memit(self, line='', cell=None):
835865
836866 _func_exec (setup , self .shell .user_ns )
837867
838- mem_usage = 0
868+ mem_usage = []
839869 counter = 0
840870 baseline = memory_usage ()[0 ]
841871 while counter < repeat :
842872 counter += 1
843873 tmp = memory_usage ((_func_exec , (stmt , self .shell .user_ns )),
844874 timeout = timeout , interval = interval , max_usage = True ,
845875 include_children = include_children )
846- mem_usage = max ( mem_usage , tmp [0 ])
876+ mem_usage . append ( tmp [0 ])
847877
848- if mem_usage :
849- print ('peak memory: %.02f MiB, increment: %.02f MiB' %
850- (mem_usage , mem_usage - baseline ))
851- else :
852- print ('ERROR: could not read memory usage, try with a lower interval '
853- 'or more iterations' )
878+ result = MemitResult (mem_usage , baseline , repeat , timeout , interval ,
879+ include_children )
880+
881+ if not quiet :
882+ if mem_usage :
883+ print (result )
884+ else :
885+ print ('ERROR: could not read memory usage, try with a lower interval '
886+ 'or more iterations' )
887+
888+ if return_result :
889+ return result
854890
855891 @classmethod
856892 def register_magics (cls , ip ):
0 commit comments