Skip to content

Commit 781ac80

Browse files
BabarberousseUbuntu
authored andcommitted
fix #249 decrement not displayed
1 parent 8a8a402 commit 781ac80

3 files changed

Lines changed: 106 additions & 18 deletions

File tree

README.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ this would result in::
6464

6565
Output will follow::
6666

67-
Line # Mem usage Increment Line Contents
68-
==============================================
69-
3 @profile
70-
4 5.97 MB 0.00 MB def my_func():
71-
5 13.61 MB 7.64 MB a = [1] * (10 ** 6)
72-
6 166.20 MB 152.59 MB b = [2] * (2 * 10 ** 7)
73-
7 13.61 MB -152.59 MB del b
74-
8 13.61 MB 0.00 MB return a
67+
Line # Mem usage Increment Occurences Line Contents
68+
============================================================
69+
3 38.816 MiB 38.816 MiB 1 @profile
70+
4 def my_func():
71+
5 46.492 MiB 7.676 MiB 1 a = [1] * (10 ** 6)
72+
6 199.117 MiB 152.625 MiB 1 b = [2] * (2 * 10 ** 7)
73+
7 46.629 MiB -152.488 MiB 1 del b
74+
8 46.629 MiB 0.000 MiB 1 return a
7575

7676

7777
The first column represents the line number of the code that has been

memory_profiler.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
280280
to this file instead of stored in memory and returned at the end of
281281
the subprocess. Useful for long-running processes.
282282
Implies timestamps=True.
283-
283+
284284
max_iterations : int
285285
Limits the number of iterations (calls to the process being monitored). Relevent
286-
when the process is a python function.
286+
when the process is a python function.
287287
288288
Returns
289289
-------
@@ -357,7 +357,7 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
357357
raise
358358

359359
p.join(5 * interval)
360-
360+
361361
if (n_measurements > 4) or (current_iter == max_iter) or (interval < 1e-6):
362362
break
363363
interval /= 10.
@@ -643,7 +643,12 @@ def trace(self, code, lineno, prev_lineno):
643643

644644
prev_line_value = self[code].get(prev_lineno, None) if prev_lineno else None
645645
prev_line_memory = prev_line_value[1] if prev_line_value else 0
646-
self[code][lineno] = (max(previous_inc, memory-prev_line_memory), max(memory, previous_memory))
646+
occ_count = self[code][lineno][2] + 1 if lineno in self[code] else 1
647+
self[code][lineno] = (
648+
previous_inc + (memory - prev_line_memory),
649+
max(memory, previous_memory),
650+
occ_count,
651+
)
647652

648653
def items(self):
649654
"""Iterate on the toplevel code blocks."""
@@ -800,10 +805,10 @@ def disable(self):
800805
def show_results(prof, stream=None, precision=1):
801806
if stream is None:
802807
stream = sys.stdout
803-
template = '{0:>6} {1:>12} {2:>12} {3:<}'
808+
template = '{0:>6} {1:>12} {2:>12} {3:>10} {4:<}'
804809

805810
for (filename, lines) in prof.code_map.items():
806-
header = template.format('Line #', 'Mem usage', 'Increment',
811+
header = template.format('Line #', 'Mem usage', 'Increment', 'Occurences',
807812
'Line Contents')
808813

809814
stream.write(u'Filename: ' + filename + '\n\n')
@@ -817,13 +822,15 @@ def show_results(prof, stream=None, precision=1):
817822
for (lineno, mem) in lines:
818823
if mem:
819824
inc = mem[0]
820-
mem = mem[1]
821-
mem = template_mem.format(mem)
825+
total_mem = mem[1]
826+
total_mem = template_mem.format(total_mem)
827+
occurences = mem[2]
822828
inc = template_mem.format(inc)
823829
else:
824-
mem = u''
830+
total_mem = u''
825831
inc = u''
826-
tmp = template.format(lineno, mem, inc, all_lines[lineno - 1])
832+
occurences = u''
833+
tmp = template.format(lineno, total_mem, inc, occurences, all_lines[lineno - 1])
827834
stream.write(to_str(tmp))
828835
stream.write(u'\n\n')
829836

test/test_increment_display.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
3+
from memory_profiler import LineProfiler, profile, show_results
4+
from io import StringIO
5+
6+
7+
class TestIncrementDisplay(unittest.TestCase):
8+
"""Tests memory incrementation / decrementation display"""
9+
10+
def test_loop_count(self):
11+
12+
def some_loop():
13+
for i in range(12): # line -2
14+
a = 1 # line -1
15+
16+
profiler = LineProfiler()
17+
wrapped = profiler(some_loop)
18+
wrapped()
19+
show_results(profiler)
20+
for_line = list(list(profiler.code_map.values())[0].values())[-2]
21+
looped_instruction = list(list(profiler.code_map.values())[0].values())[-1]
22+
23+
self.assertEqual(for_line[2], 13)
24+
self.assertEqual(looped_instruction[2], 12)
25+
26+
def test_normal_incr(self):
27+
28+
def normal_incr():
29+
use_some_memory = [1] * (10 ** 6)
30+
31+
profiler = LineProfiler()
32+
wrapped = profiler(normal_incr)
33+
wrapped()
34+
35+
show_results(profiler)
36+
results = list(list(profiler.code_map.values())[0].values())[-1]
37+
38+
self.assertGreater(results[0], 0)
39+
self.assertGreater(results[1], results[0])
40+
self.assertEqual(results[2], 1)
41+
42+
def test_loop_incr(self):
43+
44+
def loop_incr():
45+
a = []
46+
b = [2] * (2 * 10 ** 7) # line -4
47+
for i in range(3):
48+
c = [2] * (2 * 10 ** 7) # line -2
49+
a.append(c)
50+
51+
profiler = LineProfiler()
52+
wrapped = profiler(loop_incr)
53+
wrapped()
54+
55+
show_results(profiler)
56+
b_line = list(list(profiler.code_map.values())[0].values())[-4]
57+
c_line = list(list(profiler.code_map.values())[0].values())[-2]
58+
self.assertAlmostEqual(b_line[2] * 3, c_line[2], delta=1)
59+
self.assertEqual(c_line[2], 3)
60+
61+
def test_decr(self):
62+
63+
def del_stuff():
64+
b = [2] * (2 * 10 ** 7)
65+
del b
66+
67+
profiler = LineProfiler()
68+
wrapped = profiler(del_stuff)
69+
wrapped()
70+
71+
show_results(profiler)
72+
b_line = list(list(profiler.code_map.values())[0].values())[-2]
73+
del_line = list(list(profiler.code_map.values())[0].values())[-1]
74+
75+
self.assertGreater(0, del_line[0])
76+
self.assertGreater(del_line[1], 0)
77+
self.assertAlmostEqual(-del_line[0], b_line[0], delta=1)
78+
79+
80+
if __name__ == '__main__':
81+
unittest.main()

0 commit comments

Comments
 (0)