def test_speed(self): mod_name = 'list_speed'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = list(range(1000000)) code = """ int v, sum = 0; for(int i = 0; i < a.len(); i++) { v = a[i]; if (v % 2) sum += v; else sum -= v; } return_val = sum; """ with_cxx = ext_tools.ext_function('with_cxx',code,['a']) mod.add_function(with_cxx) code = """ long vv, sum = 0; PyObject *v; for(int i = 0; i < a.len(); i++) { v = PyList_GetItem(py_a,i); //didn't set error here -- just speed test vv = PyLong_AsLong(v); if (vv % 2) sum += vv; else sum -= vv; } return_val = sum; """ no_checking = ext_tools.ext_function('no_checking',code,['a']) mod.add_function(no_checking) mod.compile(location=test_dir, compiler=self.compiler) loaded_module = __import__(mod_name) with_cxx = loaded_module.with_cxx no_checking = loaded_module.no_checking t1 = time.time() sum1 = with_cxx(a) t2 = time.time() debug_print('speed test for list access') debug_print('compiler:', self.compiler) debug_print('scxx:', t2 - t1) t1 = time.time() sum2 = no_checking(a) t2 = time.time() debug_print('C, no checking:', t2 - t1) sum3 = 0 t1 = time.time() for i in a: if i % 2: sum3 += i else: sum3 -= i t2 = time.time() debug_print('python:', t2 - t1) assert_(sum1 == sum2 and sum1 == sum3)
def test_speed(self): mod_name = 'list_speed'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = list(range(1000000)) code = """ int v, sum = 0; for(int i = 0; i < a.len(); i++) { v = a[i]; if (v % 2) sum += v; else sum -= v; } return_val = sum; """ with_cxx = ext_tools.ext_function('with_cxx',code,['a']) mod.add_function(with_cxx) code = """ int vv, sum = 0; PyObject *v; for(int i = 0; i < a.len(); i++) { v = PyList_GetItem(py_a,i); //didn't set error here -- just speed test vv = py_to_int(v,"list item"); if (vv % 2) sum += vv; else sum -= vv; } return_val = sum; """ no_checking = ext_tools.ext_function('no_checking',code,['a']) mod.add_function(no_checking) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import with_cxx, no_checking') t1 = time.time() sum1 = with_cxx(a) t2 = time.time() debug_print('speed test for list access') debug_print('compiler:', self.compiler) debug_print('scxx:', t2 - t1) t1 = time.time() sum2 = no_checking(a) t2 = time.time() debug_print('C, no checking:', t2 - t1) sum3 = 0 t1 = time.time() for i in a: if i % 2: sum3 += i else: sum3 -= i t2 = time.time() debug_print('python:', t2 - t1) assert_(sum1 == sum2 and sum1 == sum3)
def test_hash(self): class Foo: def __hash__(self): return 123 a = Foo() res = inline_tools.inline('return_val = a.hash(); ',['a']) debug_print('hash:', res) assert_equal(res,123)
def test_stringio(self): file_imposter = StringIO() code = """ py::object val = "how now brown cow"; val.print(file_imposter); """ res = inline_tools.inline(code,['file_imposter']) debug_print(file_imposter.getvalue()) assert_equal(file_imposter.getvalue(),"'how now brown cow'")
def test_stringio(self): import cStringIO file_imposter = cStringIO.StringIO() code = """ py::object val = "how now brown cow"; val.print(file_imposter); """ res = inline_tools.inline(code,['file_imposter']) debug_print(file_imposter.getvalue()) assert_equal(file_imposter.getvalue(),"'how now brown cow'")
def test_access_speed(self): N = 1000000 debug_print('%s access -- val = a[i] for N =', (self.seq_type, N)) a = self.seq_type([0]) * N val = 0 t1 = time.time() for i in xrange(N): val = a[i] t2 = time.time() debug_print('python1:', t2 - t1) t1 = time.time() for i in a: val = i t2 = time.time() debug_print('python2:', t2 - t1) code = """ const int N = a.length(); py::object val; for(int i=0; i < N; i++) val = a[i]; """ # compile not included in timing inline_tools.inline(code,['a']) t1 = time.time() inline_tools.inline(code,['a']) t2 = time.time() debug_print('weave:', t2 - t1)
def test_access_speed(self): N = 1000000 debug_print('%s access -- val = a[i] for N =', (self.seq_type, N)) a = self.seq_type([0]) * N val = 0 t1 = time.time() for i in xrange(N): val = a[i] t2 = time.time() debug_print('python1:', t2 - t1) t1 = time.time() for i in a: val = i t2 = time.time() debug_print('python2:', t2 - t1) code = """ const int N = a.length(); py::object val; for(int i=0; i < N; i++) val = a[i]; """ # compile not included in timing inline_tools.inline(code, ['a']) t1 = time.time() inline_tools.inline(code, ['a']) t2 = time.time() debug_print('weave:', t2 - t1)
def generic_2d(self,expr,typ): # The complex testing is pretty lame... ast = parser.suite(expr) arg_list = harvest_variables(ast.tolist()) all_sizes = [(10,10), (50,50), (100,100), (500,500), (1000,1000)] debug_print('\nExpression:', expr) with TempdirBlitz(): for size in all_sizes: arg_dict = {} for arg in arg_list: arg_dict[arg] = random.normal(0,1,size).astype(typ) # set imag part of complex values to non-zero value try: arg_dict[arg].imag = arg_dict[arg].real except: pass debug_print('Run:', size,typ) standard,compiled = self.generic_check(expr,arg_dict,type,size) try: speed_up = standard/compiled except: speed_up = -1. debug_print("1st run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard,compiled,speed_up)) standard,compiled = self.generic_check(expr,arg_dict,type,size) try: speed_up = standard/compiled except: speed_up = -1. debug_print("2nd run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard,compiled,speed_up))
def generic_2d(self, expr, typ): # The complex testing is pretty lame... ast = parser.suite(expr) arg_list = harvest_variables(ast.tolist()) all_sizes = [(10, 10), (50, 50), (100, 100), (500, 500), (1000, 1000)] debug_print('\nExpression:', expr) with TempdirBlitz(): for size in all_sizes: arg_dict = {} for arg in arg_list: arg_dict[arg] = random.normal(0, 1, size).astype(typ) # set imag part of complex values to non-zero value try: arg_dict[arg].imag = arg_dict[arg].real except: pass debug_print('Run:', size, typ) standard, compiled = self.generic_check( expr, arg_dict, type, size) try: speed_up = standard / compiled except: speed_up = -1. debug_print("1st run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard, compiled, speed_up)) standard, compiled = self.generic_check( expr, arg_dict, type, size) try: speed_up = standard / compiled except: speed_up = -1. debug_print("2nd run(numpy,compiled,speed up): %3.4f, %3.4f, " "%3.4f" % (standard, compiled, speed_up))
def test_inline(self): #TODO: THIS NEEDS TO MOVE TO THE INLINE TEST SUITE a = Foo() a.b = 12345 code = """ throw_error(PyExc_AttributeError,"bummer"); """ try: before = sys.getrefcount(a) inline_tools.inline(code,['a']) except AttributeError: after = sys.getrefcount(a) try: inline_tools.inline(code,['a']) except: after2 = sys.getrefcount(a) debug_print("after and after2 should be equal in the following") debug_print('before, after, after2:', before, after, after2)
def test_int_add_speed(self): N = 1000000 debug_print('int add -- b[i] = a[i] + 1 for N =', N) a = [0] * N desired = [1] * N t1 = time.time() for i in xrange(N): desired[i] = a[i] + 1 t2 = time.time() debug_print('python:', t2 - t1) a = [0] * N b = [0] * N code = """ const int N = a.length(); for(int i=0; i < N; i++) b[i] = (int)a[i] + 1; """ # compile not included in timing inline_tools.inline(code, ['a', 'b']) t1 = time.time() inline_tools.inline(code, ['a', 'b']) t2 = time.time() debug_print('weave:', t2 - t1) assert_(b == desired)
def test_string_add_speed(self): N = 1000000 debug_print('string add -- b[i] = a[i] + "blah" for N =', N) a = ["blah"] * N desired = [1] * N t1 = time.time() for i in xrange(N): desired[i] = a[i] + 'blah' t2 = time.time() debug_print('python:', t2 - t1) a = ["blah"] * N b = [1] * N code = """ const int N = a.length(); std::string blah = std::string("blah"); for(int i=0; i < N; i++) b[i] = convert_to_string(a[i],"a") + blah; """ # compile not included in timing inline_tools.inline(code, ['a', 'b']) t1 = time.time() inline_tools.inline(code, ['a', 'b']) t2 = time.time() debug_print('weave:', t2 - t1) assert_(b == desired)
def test_access_set_speed(self): N = 1000000 debug_print('%s access/set -- b[i] = a[i] for N =', (self.seq_type, N)) a = self.seq_type([0]) * N # b is always a list so we can assign to it. b = [1] * N t1 = time.time() for i in xrange(N): b[i] = a[i] t2 = time.time() debug_print('python:', t2 - t1) a = self.seq_type([0]) * N b = [1] * N code = """ const int N = a.length(); for(int i=0; i < N; i++) b[i] = a[i]; """ # compile not included in timing inline_tools.inline(code, ['a', 'b']) t1 = time.time() inline_tools.inline(code, ['a', 'b']) t2 = time.time() debug_print('weave:', t2 - t1) assert_(list(b) == list(a))
def test_int_add_speed(self): N = 1000000 debug_print('int add -- b[i] = a[i] + 1 for N =', N) a = [0] * N desired = [1] * N t1 = time.time() for i in xrange(N): desired[i] = a[i] + 1 t2 = time.time() debug_print('python:', t2 - t1) a = [0] * N b = [0] * N code = """ const int N = a.length(); for(int i=0; i < N; i++) b[i] = (int)a[i] + 1; """ # compile not included in timing inline_tools.inline(code,['a','b']) t1 = time.time() inline_tools.inline(code,['a','b']) t2 = time.time() debug_print('weave:', t2 - t1) assert_(b == desired)
def test_string_add_speed(self): N = 1000000 debug_print('string add -- b[i] = a[i] + "blah" for N =', N) a = ["blah"] * N desired = [1] * N t1 = time.time() for i in xrange(N): desired[i] = a[i] + 'blah' t2 = time.time() debug_print('python:', t2 - t1) a = ["blah"] * N b = [1] * N code = """ const int N = a.length(); std::string blah = std::string("blah"); for(int i=0; i < N; i++) b[i] = convert_to_string(a[i],"a") + blah; """ # compile not included in timing inline_tools.inline(code,['a','b']) t1 = time.time() inline_tools.inline(code,['a','b']) t2 = time.time() debug_print('weave:', t2 - t1) assert_(b == desired)
def test_access_set_speed(self): N = 1000000 debug_print('%s access/set -- b[i] = a[i] for N =', (self.seq_type,N)) a = self.seq_type([0]) * N # b is always a list so we can assign to it. b = [1] * N t1 = time.time() for i in xrange(N): b[i] = a[i] t2 = time.time() debug_print('python:', t2 - t1) a = self.seq_type([0]) * N b = [1] * N code = """ const int N = a.length(); for(int i=0; i < N; i++) b[i] = a[i]; """ # compile not included in timing inline_tools.inline(code,['a','b']) t1 = time.time() inline_tools.inline(code,['a','b']) t2 = time.time() debug_print('weave:', t2 - t1) assert_(list(b) == list(a))