1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | A program to test differences in time and memory usage between Numeric |
---|
5 | and numpy code. |
---|
6 | |
---|
7 | Due to the above, this will run only on a machine that has both of the |
---|
8 | above packages installed. |
---|
9 | ''' |
---|
10 | |
---|
11 | |
---|
12 | import sys |
---|
13 | import os |
---|
14 | import time |
---|
15 | import gc |
---|
16 | import Numeric |
---|
17 | import numpy |
---|
18 | |
---|
19 | |
---|
20 | ARRAY_SIZE = 1000 * 1000 |
---|
21 | LOOP_SIZE = 1000 |
---|
22 | |
---|
23 | |
---|
24 | def mem_usage(): |
---|
25 | '''Get memory usage (virtual) in KiB.''' |
---|
26 | |
---|
27 | _scale = {'KB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024, |
---|
28 | 'kB': 1024, 'mB': 1024*1024, 'gB': 1024*1024*1024} |
---|
29 | |
---|
30 | if sys.platform != 'win32': |
---|
31 | _proc_status = '/proc/%d/status' % os.getpid() |
---|
32 | |
---|
33 | def _VmB(VmKey): |
---|
34 | '''Get number of virtual bytes used.''' |
---|
35 | |
---|
36 | # get pseudo file /proc/<pid>/status |
---|
37 | try: |
---|
38 | t = open(_proc_status) |
---|
39 | v = t.read() |
---|
40 | t.close() |
---|
41 | except IOError: |
---|
42 | return 0.0 |
---|
43 | |
---|
44 | # get VmKey line, eg: 'VmRSS: 999 kB\n ... |
---|
45 | i = v.index(VmKey) |
---|
46 | v = v[i:].split(None, 3) |
---|
47 | if len(v) < 3: |
---|
48 | return 0.0 |
---|
49 | |
---|
50 | # convert Vm value to bytes |
---|
51 | return float(v[1]) * _scale[v[2]] |
---|
52 | |
---|
53 | return int(_VmB('VmSize:')/_scale['KB']) |
---|
54 | else: |
---|
55 | # Windows code from: http://code.activestate.com/recipes/511491/ |
---|
56 | try: |
---|
57 | import ctypes |
---|
58 | import _winreg |
---|
59 | except: |
---|
60 | log(level, 'Windows resource usage not available') |
---|
61 | return |
---|
62 | |
---|
63 | kernel32 = ctypes.windll.kernel32 |
---|
64 | c_ulong = ctypes.c_ulong |
---|
65 | c_ulonglong = ctypes.c_ulonglong |
---|
66 | class MEMORYSTATUSEX(ctypes.Structure): |
---|
67 | _fields_ = [('dwLength', c_ulong), |
---|
68 | ('dwMemoryLoad', c_ulong), |
---|
69 | ('ullTotalPhys', c_ulonglong), |
---|
70 | ('ullAvailPhys', c_ulonglong), |
---|
71 | ('ullTotalPageFile', c_ulonglong), |
---|
72 | ('ullAvailPageFile', c_ulonglong), |
---|
73 | ('ullTotalVirtual', c_ulonglong), |
---|
74 | ('ullAvailVirtual', c_ulonglong), |
---|
75 | ('ullAvailExtendedVirtual', c_ulonglong) |
---|
76 | ] |
---|
77 | |
---|
78 | memoryStatusEx = MEMORYSTATUSEX() |
---|
79 | memoryStatusEx.dwLength = ctypes.sizeof(MEMORYSTATUSEX) |
---|
80 | kernel32.GlobalMemoryStatusEx(ctypes.byref(memoryStatusEx)) |
---|
81 | |
---|
82 | return int(memoryStatusEx.ullTotalPhys/_scale['KB']) |
---|
83 | |
---|
84 | def test_usage(module, A, B): |
---|
85 | # do some numeric calculations |
---|
86 | C = B |
---|
87 | for i in xrange(LOOP_SIZE): |
---|
88 | C = 2.6*A + B + C + i |
---|
89 | |
---|
90 | # Do numpy work |
---|
91 | start_time = time.time() |
---|
92 | start_mem = mem_usage() |
---|
93 | |
---|
94 | A = numpy.ones(ARRAY_SIZE, numpy.float) |
---|
95 | B = numpy.ones(ARRAY_SIZE, numpy.float) |
---|
96 | for i in xrange(ARRAY_SIZE): |
---|
97 | A[i] *= float(i) |
---|
98 | B[i] *= float(i) |
---|
99 | |
---|
100 | test_usage(numpy, A, B) |
---|
101 | |
---|
102 | stop_mem = mem_usage() |
---|
103 | stop_time = time.time() |
---|
104 | |
---|
105 | del A, B |
---|
106 | gc.collect() |
---|
107 | |
---|
108 | delta_time = stop_time - start_time |
---|
109 | delta_mem = stop_mem - start_mem |
---|
110 | print(' numpy: %.1f s, %d KiB' % (delta_time, delta_mem)) |
---|
111 | |
---|
112 | # Do Numeric work |
---|
113 | start_time = time.time() |
---|
114 | start_mem = mem_usage() |
---|
115 | |
---|
116 | A = Numeric.ones(ARRAY_SIZE, Numeric.Float) |
---|
117 | B = Numeric.ones(ARRAY_SIZE, Numeric.Float) |
---|
118 | for i in xrange(ARRAY_SIZE): |
---|
119 | A[i] *= float(i) |
---|
120 | B[i] *= float(i) |
---|
121 | |
---|
122 | test_usage(Numeric, A, B) |
---|
123 | |
---|
124 | stop_mem = mem_usage() |
---|
125 | stop_time = time.time() |
---|
126 | |
---|
127 | del A, B |
---|
128 | gc.collect() |
---|
129 | |
---|
130 | delta_time = stop_time - start_time |
---|
131 | delta_mem = stop_mem - start_mem |
---|
132 | print('Numeric: %.1f s, %d KiB' % (delta_time, delta_mem)) |
---|
133 | |
---|