|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | | -import os, sys, time |
| 3 | +import os, sys, time, gc |
4 | 4 | import matplotlib |
5 | | -#matplotlib.interactive(True) |
6 | | -#matplotlib.use('Cairo') |
7 | 5 | matplotlib.use('Agg') |
8 | | -from pylab import * |
9 | | - |
10 | | - |
11 | | -def report_memory(i): |
12 | | - pid = os.getpid() |
13 | | - a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines() |
14 | | - print i, ' ', a2[1], |
15 | | - return int(a2[1].split()[1]) |
16 | | - |
17 | 6 |
|
| 7 | +from matplotlib.cbook import report_memory |
| 8 | +import matplotlib.numerix as nx |
| 9 | +from pylab import figure, show, close |
18 | 10 |
|
19 | 11 | # take a memory snapshot on indStart and compare it with indEnd |
20 | 12 |
|
| 13 | +rand = nx.mlab.rand |
| 14 | + |
21 | 15 | indStart, indEnd = 30, 201 |
22 | 16 | for i in range(indEnd): |
23 | 17 |
|
24 | | - figure(1); clf() |
| 18 | + fig = figure(1) |
| 19 | + fig.clf() |
| 20 | + |
| 21 | + |
| 22 | + t1 = nx.arange(0.0, 2.0, 0.01) |
| 23 | + y1 = nx.sin(2*nx.pi*t1) |
| 24 | + y2 = rand(len(t1)) |
| 25 | + X = rand(50,50) |
25 | 26 |
|
26 | | - subplot(221) |
27 | | - t1 = arange(0.0, 2.0, 0.01) |
28 | | - y = sin(2*pi*t1) |
29 | | - plot(t1,y,'-') |
30 | | - plot(t1, rand(len(t1)), 's', hold=True) |
| 27 | + ax = fig.add_subplot(221) |
| 28 | + ax.plot(t1, y1, '-') |
| 29 | + ax.plot(t1, y2, 's') |
31 | 30 |
|
32 | 31 |
|
33 | | - subplot(222) |
34 | | - X = rand(50,50) |
| 32 | + ax = fig.add_subplot(222) |
| 33 | + ax.imshow(X) |
35 | 34 |
|
36 | | - imshow(X) |
37 | | - subplot(223) |
38 | | - scatter(rand(50), rand(50), s=100*rand(50), c=rand(50)) |
39 | | - subplot(224) |
40 | | - pcolor(10*rand(50,50)) |
41 | | - #ion() |
42 | | - #draw() |
| 35 | + ax = fig.add_subplot(223) |
| 36 | + ax.scatter(rand(50), rand(50), s=100*rand(50), c=rand(50)) |
43 | 37 |
|
44 | | - #ioff() |
| 38 | + ax = fig.add_subplot(224) |
| 39 | + ax.pcolor(10*rand(50,50)) |
45 | 40 |
|
46 | | - #fd = file('tmp%d' % i, 'wb') |
47 | | - #savefig(fd, dpi = 75) |
48 | | - #fd.close() |
49 | | - savefig('tmp%d' % i, dpi = 75) |
| 41 | + fig.savefig('tmp%d' % i, dpi = 75) |
50 | 42 | close(1) |
51 | | - #break |
52 | 43 |
|
| 44 | + gc.collect() |
53 | 45 | val = report_memory(i) |
| 46 | + print i, val |
54 | 47 | if i==indStart: start = val # wait a few cycles for memory usage to stabilize |
55 | 48 |
|
56 | 49 | end = val |
57 | 50 | print 'Average memory consumed per loop: %1.4fk bytes\n' % ((end-start)/float(indEnd-indStart)) |
58 | 51 |
|
59 | | -""" |
60 | | -Average memory consumed per loop: 0.0053k bytes |
61 | | -""" |
0 commit comments