# array module example import sample import array a = array.array('d', [1, -3, 4, 7, 2, 0]) print(a) sample.clip(a, 1, 4, a) print(a) # numpy example import numpy b = numpy.random.uniform(-10, 10, size=1000000) print(b) c = numpy.zeros_like(b) print(c) sample.clip(b, -5, 5, c) print(c) print(min(c)) print(max(c)) # Timing test from timeit import timeit print('numpy.clip') print( timeit('numpy.clip(b,-5,5,c)', 'from __main__ import b,c,numpy', number=1000)) print('sample.clip') print( timeit('sample.clip(b,-5,5,c)', 'from __main__ import b,c,sample', number=1000))
# array module example import sample import array a = array.array('d',[1,-3,4,7,2,0]) print(a) sample.clip(a,1,4,a) print(a) # numpy example import numpy b = numpy.random.uniform(-10,10,size=1000000) print(b) c = numpy.zeros_like(b) print(c) sample.clip(b,-5,5,c) print(c) print(min(c)) print(max(c)) # Timing test from timeit import timeit print('numpy.clip') print(timeit('numpy.clip(b,-5,5,c)', 'from __main__ import b,c,numpy', number=1000)) print('sample.clip') print(timeit('sample.clip(b,-5,5,c)', 'from __main__ import b,c,sample', number=1000)) print('sample.clip_fast') print(timeit('sample.clip_fast(b,-5,5,c)', 'from __main__ import b,c,sample', number=1000)) # 2D test d = numpy.random.uniform(-10,10,size=(1000,1000))
print( 'numpy takes:', timeit.timeit('numpy.clip(a,-5,5,c)', """ import numpy import sample a = numpy.random.uniform(-10,10,size=1000000) c = numpy.zeros_like(a) numpy.clip(a,-5,5,c) """, number=1000)) print( 'cython clip takes:', timeit.timeit('numpy.clip(a,-5,5,c)', """ import numpy import sample a = numpy.random.uniform(-10,10,size=1000000) c = numpy.zeros_like(a) sample.clip(a,-5,5,c) """, number=1000)) print( 'cython clip2 takes:', timeit.timeit('numpy.clip(a,-5,5,c)', """ import numpy import sample
import sample import numpy as np _same_distance = np.array([4, 1, 1, 5, 3], dtype=float) _diff_distance = np.array([1, 1, 1, 6, 3], dtype=float) # should output 0.6 print(sample.clip(_same_distance, 2.0, _diff_distance)) print("test clip") _same_distance = np.array([4, 1, 1, 5, 3], dtype=float) _diff_distance = np.array([1, 1, 1, 6, 3], dtype=float) # should output 0.6 print(sample.clip(_same_distance, 2.0, _diff_distance))
def run(self): begin = datetime.datetime.now() acc = sample.clip(array.array('d', self._same_distance), self.threshold, array.array('d', self._diff_distance)) accuracy.dic[self.threshold] = acc end = datetime.datetime.now() print(end - begin, acc)