Changeset 7245
- Timestamp:
- Jun 23, 2009, 7:39:39 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/numpy_misc/tools/test_numpy_numeric/test_numpy_numeric.py
r7243 r7245 19 19 20 20 ARRAY_SIZE = 1000 * 1000 21 LOOP_SIZE = 1000 21 22 23 # Numeric version 24 def Numeric_ensure_numeric(A, typecode=None): 25 """Ensure that sequence is a numeric array. 26 27 Inputs: 28 A: Sequence. If A is already a Numeric array it will be returned 29 unaltered 30 If not, an attempt is made to convert it to a Numeric 31 array 32 A: Scalar. Return 0-dimensional array of length 1, containing that value 33 A: String. Array of ASCII values 34 typecode: Numeric type. If specified, use this in the conversion. 35 If not, let Numeric decide 36 37 This function is necessary as array(A) can cause memory overflow. 38 """ 39 40 if typecode is None: 41 if type(A) == Numeric.ArrayType: 42 return A 43 else: 44 return Numeric.array(A) 45 else: 46 if type(A) == Numeric.ArrayType: 47 if A.typecode() == typecode: 48 return A 49 else: 50 return Numeric.array(A, typecode) 51 else: 52 return Numeric.array(A, typecode) 53 54 55 # numpy version 56 def numpy_ensure_numeric(A, typecode=None): 57 """Ensure that sequence is a numeric array. 58 59 Inputs: 60 A: Sequence. If A is already a numeric array it will be returned 61 unaltered 62 If not, an attempt is made to convert it to a numeric 63 array 64 A: Scalar. Return 0-dimensional array containing that value. Note 65 that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy. 66 A: String. Array of ASCII values (numpy can't handle this) 67 68 typecode: numeric type. If specified, use this in the conversion. 69 If not, let numeric package decide. 70 typecode will always be one of num.float, num.int, etc. 71 72 Note that num.array(A, dtype) will sometimes copy. Use 'copy=False' to 73 copy only when required. 74 75 This function is necessary as array(A) can cause memory overflow. 76 """ 77 78 # if isinstance(A, basestring): 79 # msg = 'Sorry, cannot handle strings in ensure_numeric()' 80 # raise Exception, msg 81 82 if typecode is None: 83 if isinstance(A, numpy.ndarray): 84 return A 85 else: 86 return numpy.array(A) 87 else: 88 return numpy.array(A, dtype=typecode, copy=False) 22 89 23 90 … … 82 149 return int(memoryStatusEx.ullTotalPhys/_scale['KB']) 83 150 84 def test_usage(module, A, B): 151 152 def test_usage(module, f, en, ls): 153 start_time = time.time() 154 start_mem = mem_usage() 155 156 A = module.ones(ARRAY_SIZE, f) 157 B = module.ones(ARRAY_SIZE, f) 158 for i in xrange(ls): 159 A[i] *= float(i) 160 B[i] *= float(i) 161 85 162 # do some numeric calculations 86 C = B 87 for i in xrange(LOOP_SIZE): 88 C = 2.6*A + B + C + i 163 A = en(A) 164 B = en(B) 165 C = module.ones(ARRAY_SIZE, f) 166 C = en(C) 167 for i in xrange(ls): 168 C = en(2.6*A + B + C + i) 89 169 90 # Do numpy work 91 start_time = time.time() 92 start_mem = mem_usage() 170 stop_mem = mem_usage() 171 stop_time = time.time() 93 172 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) 173 delta_time = stop_time - start_time 174 delta_mem = stop_mem - start_mem 99 175 100 test_usage(numpy, A, B) 176 del A, B, C 177 gc.collect() 101 178 102 stop_mem = mem_usage() 103 stop_time = time.time() 179 return (delta_time, delta_mem) 104 180 105 del A, B 106 gc.collect() 181 for loop_size in (10, 100, 1000): 182 # Do numpy work 183 (t, m) = test_usage(numpy, numpy.float, numpy_ensure_numeric, loop_size) 184 print(' numpy %4d loops: %5.1f s, %d KiB' % (loop_size, t, m)) 107 185 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)) 186 # Do Numeric work 187 (t, m) = test_usage(Numeric, Numeric.Float, Numeric_ensure_numeric, 188 loop_size) 189 print('Numeric %4d loops: %5.1f s, %d KiB' % (loop_size, t, m)) 111 190 112 # Do Numeric work113 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, B128 gc.collect()129 130 delta_time = stop_time - start_time131 delta_mem = stop_mem - start_mem132 print('Numeric: %.1f s, %d KiB' % (delta_time, delta_mem))133
Note: See TracChangeset
for help on using the changeset viewer.