source: branches/numpy_misc/tools/test_numpy_numeric/test_numpy_numeric.py @ 7244

Last change on this file since 7244 was 7243, checked in by rwilson, 16 years ago

First stab at a Numeric/numpy comparison program.

File size: 3.4 KB
Line 
1#!/usr/bin/env python
2
3'''
4A program to test differences in time and memory usage between Numeric
5and numpy code.
6
7Due to the above, this will run only on a machine that has both of the
8above packages installed.
9'''
10
11
12import sys
13import os
14import time
15import gc
16import Numeric
17import numpy
18
19
20ARRAY_SIZE = 1000 * 1000
21LOOP_SIZE = 1000
22
23
24def 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
84def 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
91start_time = time.time()
92start_mem = mem_usage()
93
94A = numpy.ones(ARRAY_SIZE, numpy.float)
95B = numpy.ones(ARRAY_SIZE, numpy.float)
96for i in xrange(ARRAY_SIZE):
97    A[i] *= float(i)
98    B[i] *= float(i)
99
100test_usage(numpy, A, B)
101
102stop_mem = mem_usage()
103stop_time = time.time()
104
105del A, B
106gc.collect()
107
108delta_time = stop_time - start_time
109delta_mem = stop_mem - start_mem
110print('  numpy: %.1f s, %d KiB' % (delta_time, delta_mem))
111
112# Do Numeric work
113start_time = time.time()
114start_mem = mem_usage()
115
116A = Numeric.ones(ARRAY_SIZE, Numeric.Float)
117B = Numeric.ones(ARRAY_SIZE, Numeric.Float)
118for i in xrange(ARRAY_SIZE):
119    A[i] *= float(i)
120    B[i] *= float(i)
121
122test_usage(Numeric, A, B)
123
124stop_mem = mem_usage()
125stop_time = time.time()
126
127del A, B
128gc.collect()
129
130delta_time = stop_time - start_time
131delta_mem = stop_mem - start_mem
132print('Numeric: %.1f s, %d KiB' % (delta_time, delta_mem))
133
Note: See TracBrowser for help on using the repository browser.