def _efficiency(): print('\nPerform some efficiency tests (this might take some time...):') formula = 'sin(x) + x**3 + 2*x' formula_wprm = formula + ' + A*B' def s0(x): return sin(x) + x**3 + 2 * x s1 = StringFunction_v1(formula) s2 = StringFunction_v2(formula) # compiled s3 = StringFunction_v3(formula_wprm, set_parameters='A=0; B=0') s4 = StringFunction_v4(formula) s5 = StringFunction_v5(formula, independent_variables=('x', ), A=0, B=0) s6 = StringFunction(formula, independent_variables=('x', ), A=0, B=0) s7 = s6.__call__ x = 0.9 # verification first: values = [s(x) for s in (s0, s1, s2, s3, s4, s5, s6, s7)] print('values of %s for x=%s: %s' % (formula, x, values)) n = 400000 from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable as ET e = ET('Efficiency check of StringFunction implementations; ' 'formula=%s, n=%d' % (formula, n)) import inspect for s in s0, s1, s2, s3, s4, s5, s6, s7: if inspect.isfunction(s) or inspect.ismethod(s): name = s.__name__ else: name = s.__class__.__name__ t = timer(s, args=(x, ), repetitions=100000, comment=name) e.add(name, t) print(e) print('End of efficiency tests\n')
def timing3(n=2000, best_time=1.0): print 'Grid2Deff2.timing2: reference CPU time = %g' % best_time dx = 1.0 / n g = Grid2Deff2(dx=dx, dy=dx) # here we use straight NumPy sin in a scalar context: def myfunc1(x, y): return sin(x * y) + 8 * x def myfunc2(x, y): return math.sin(x * y) + 8 * x from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('Grid2Deff2 tests', best_time) print 'basic NumPy module:', basic_NumPy if basic_NumPy != 'Numeric': print 'only Numeric is meaningful for the C++ extension module' return t1a = timer(g.ext_gridloop1, (myfunc1, ), repetitions=1) e.add('g.ext_gridloop1, myfunc1', t1a) t1b = timer(g.ext_gridloop1, (myfunc2, ), repetitions=1) e.add('g.ext_gridloop1, myfunc2', t1b) t2a = timer(g.ext_gridloop2, (myfunc1, ), repetitions=1) e.add('g.ext_gridloop2, myfunc1', t2a) t2b = timer(g.ext_gridloop2, (myfunc2, ), repetitions=1) e.add('g.ext_gridloop2, myfunc2', t2b) print e
def timing3(n=2000, best_time=1.0): print 'Grid2Deff2.timing2: reference CPU time = %g' % best_time dx = 1.0/n g = Grid2Deff2(dx=dx, dy=dx) # here we use straight NumPy sin in a scalar context: def myfunc1(x, y): return sin(x*y) + 8*x def myfunc2(x, y): return math.sin(x*y) + 8*x from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('Grid2Deff2 tests', best_time) print 'basic NumPy module:', basic_NumPy if basic_NumPy != 'Numeric': print 'only Numeric is meaningful for the C++ extension module' return t1a = timer(g.ext_gridloop1, (myfunc1,), repetitions=1) e.add('g.ext_gridloop1, myfunc1', t1a) t1b = timer(g.ext_gridloop1, (myfunc2,), repetitions=1) e.add('g.ext_gridloop1, myfunc2', t1b) t2a = timer(g.ext_gridloop2, (myfunc1,), repetitions=1) e.add('g.ext_gridloop2, myfunc1', t2a) t2b = timer(g.ext_gridloop2, (myfunc2,), repetitions=1) e.add('g.ext_gridloop2, myfunc2', t2b) print e
def _efficiency(): print('\nPerform some efficiency tests (this might take some time...):') formula = 'sin(x) + x**3 + 2*x' formula_wprm = formula + ' + A*B' def s0(x): return sin(x) + x**3 + 2 * x s1 = StringFunction_v1(formula) s2 = StringFunction_v2(formula) # compiled s3 = StringFunction_v3(formula_wprm, set_parameters='A=0; B=0') s4 = StringFunction_v4(formula) s5 = StringFunction_v5(formula, independent_variables=('x',), A=0, B=0) s6 = StringFunction(formula, independent_variables=('x',), A=0, B=0) s7 = s6.__call__ x = 0.9 # verification first: values = [s(x) for s in (s0, s1, s2, s3, s4, s5, s6, s7)] print('values of %s for x=%s: %s' % (formula, x, values)) n = 400000 from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable as ET e = ET('Efficiency check of StringFunction implementations; ' 'formula=%s, n=%d' % (formula, n)) import inspect for s in s0, s1, s2, s3, s4, s5, s6, s7: if inspect.isfunction(s) or inspect.ismethod(s): name = s.__name__ else: name = s.__class__.__name__ t = timer(s, args=(x,), repetitions=100000, comment=name) e.add(name, t) print(e) print('End of efficiency tests\n')
def timing2(n=2000, best_time=1.0): """Time different implementations of the extension module.""" print 'Grid2Deff.timing2: reference CPU time = %g' % best_time dx = 1.0/n g = Grid2Deff(dx=dx, dy=dx) # here we use straight NumPy sin in a scalar context: def myfunc1(x, y): return sin(x*y) + 8*x def myfunc2(x, y): return math.sin(x*y) + 8*x expression1 = StringFunction('sin(x*y) + 8*x', independent_variables=('x','y'), globals=globals()) expression1_f = expression1.__call__ # for efficiency and F77 callback expression2 = StringFunction('math.sin(x*y) + 8*x', independent_variables=('x','y'), globals=globals()) expression2_f = expression2.__call__ # for efficiency and F77 callback from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('Grid2Deff tests, %dx%d grid' % (n,n), best_time) t0a = timer(g.gridloop, (myfunc1,), repetitions=1) e.add('g.gridloop, myfunc1', t0a) t0b = timer(g.gridloop, (myfunc2,), repetitions=1) e.add('g.gridloop, myfunc2', t0b) t0c = timer(g.__call__, (myfunc1,), repetitions=1) e.add('g.__call__, myfunc1', t0c) t0d = timer(g.__call__, (expression1_f,), repetitions=1) e.add('g.__call__, expression1_f', t0d) t0e = timer(g.gridloop_itemset, (myfunc2,), repetitions=1) e.add('g.gridloop_itemset, myfunc2', t0e) t1a = timer(g.ext_gridloop1, (myfunc1,), repetitions=1) e.add('g.ext_gridloop1, myfunc1', t1a) t1b = timer(g.ext_gridloop1, (myfunc2,), repetitions=1) e.add('g.ext_gridloop1, myfunc2', t1b) t2a = timer(g.ext_gridloop2, (myfunc1,), repetitions=1) e.add('g.ext_gridloop2, myfunc1', t2a) t2b = timer(g.ext_gridloop2, (myfunc2,), repetitions=1) e.add('g.ext_gridloop2, myfunc2', t2b) t3a = timer(g.ext_gridloop2, (expression1_f,), repetitions=1) e.add('g.ext_gridloop2, expression1_f', t3a) t3b = timer(g.ext_gridloop2, (expression2_f,), repetitions=1) e.add('g.ext_gridloop2, expression2_f', t3b) nrep = 20 # try the improved functions (works only for the F77 module): if 'gridloop_vec2' in dir(ext_gridloop): t4 = timer(g.ext_gridloop_vec2, (myfuncf2,), repetitions=nrep) e.add('g.ext_gridloop_vec2, myfuncf2', t4) if 'gridloop2_str' in dir(ext_gridloop): t5 = timer(g.ext_gridloop2_str, ('myfunc',), repetitions=nrep) e.add('g.ext_gridloop2_str, myfunc', t5) # try the version without allocation (first, make an a array): a = g.ext_gridloop2(myfunc1) # a has now Fortran storage t5b = timer(g.ext_gridloop_noalloc, ('myfunc', a), repetitions=nrep) e.add('g.ext_gridloop_noalloc, myfunc', t5b) # try 'inline' F77 compiled callback too: # (give F77 source for core of callback function as argument) g.ext_gridloop2_fcb_compile(str(expression1)) t6 = timer(g.ext_gridloop2_fcb, (), repetitions=nrep) e.add('g.ext_gridloop2_fcb(%s)' % repr(str(expression1)), t6) g.ext_gridloop2_fcb_ptr_compile(expression1) t6b = timer(g.ext_gridloop2_fcb_ptr, (), repetitions=nrep) e.add('g.ext_gridloop2_fcb_ptr(%s)' % repr(expression1), t6b) g.ext_gridloop2_compile(str(expression1)) t7 = timer(g.ext_gridloop2_v2, (), repetitions=nrep) e.add('g.ext_gridloop2_v2(%s)' % repr(str(expression1)), t7) # weave version: t8 = timer(g.ext_gridloop2_weave, (str(expression1),), repetitions=nrep) e.add('g.ext_gridloop2_weave(%s)' % repr(str(expression1)), t8) # psyco: g.gridloop_psyco_init(g.gridloop) if g.gridloop_psyco != g.gridloop: # has psyco t9a = timer(g.gridloop_psyco, (myfunc2,), repetitions=1) e.add('g.gridloop_psyco, myfunc2', t9a) t9b = timer(g.gridloop_psyco, (expression2_f,), repetitions=1) e.add('g.gridloop_psyco, expression2_f', t9b) g.gridloop_psyco_init(g.gridloop_itemset) if g.gridloop_psyco != g.gridloop_itemset: # has psyco t9a = timer(g.gridloop_psyco, (myfunc2,), repetitions=1) e.add('g.gridloop_psyco (itemset), myfunc2', t9a) t9b = timer(g.gridloop_psyco, (expression2_f,), repetitions=1) e.add('g.gridloop_psyco (itemset), expression2_f', t9b) # instant: g.ext_gridloop1_instant(str(expression1)) if g.gridloop1_instant is not None: a = zeros((self.nx, self.ny)) t10 = timer(g.gridloop1_instant, (a, self.nx, g.ny, g.xcoor, g.ycoor), repetitions=nrep) e.add('g.gridloop1_instant', t10) print '\n\n\n\nrun from directory', os.getcwd() print e
print 'n =', n from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('%d evaluations of arcsin(0.4)' % n) t1 = timeit.Timer('arcsin(0.4)', setup='from numpy import arcsin').timeit(n) t2 = timeit.Timer('asin(0.4)', setup='from math import asin').timeit(n) t3 = timeit.Timer('arcsin(0.4)', setup='from numarray import arcsin').timeit(n) t4 = timeit.Timer('arcsin(0.4)', setup='from Numeric import arcsin').timeit(n) t5 = timeit.Timer('arcsin(0.4)', setup='from numpy.lib.scimath import arcsin').timeit(n) e.add('numpy arcsin', t1) e.add('math asin', t2) e.add('numarray arcsin', t3) e.add('Numeric arcsin', t4) e.add('numeric.lib.scimath arcsin', t5) print e # call to function involving intrinsic functions: e = EfficiencyTable('calling arcsin, arccos inside a function') sc = 'myfunc(0.4, 0.4)' f = """ arcsin, arccos def myfunc(x, y): return x**2 + arccos(x*y)*arcsin(x) """ f2 = """ asin, acos def myfunc(x, y):
return r def somefunc_NumPy_logv(x): x_pos = where(x > 0, x, 1) r1 = log(x_pos) r = where(x < 0, 0.0, r1) return r somefunc_list = [somefuncv, somefunc_NumPy, somefunc_NumPy2, somefunc_NumPy2b, somefunc_NumPy3, somefunc_NumPy_log, somefunc_NumPy_logv] # check correctness: x = sequence(-2, 2, 2) print '\nsomefunc_* functions applied to', x for f in somefunc_list: print f(x) n = 5000000 #n = 500000 x = linspace(0, 2, n+1) print '\nperforming some timings of "somefunc*" implementations...' from scitools.EfficiencyTable import EfficiencyTable as ET from scitools.misc import timer e = ET('vectorization of "somefunc" functions with an if test, n=%d' % n) for f in somefunc_list[:-2]: # skip the last two log functions t = timer(f, (x,), repetitions=1) e.add(f.__name__, t) print e print 'end of', sys.argv[0]
def timing2(n=2000, best_time=1.0): """Time different implementations of the extension module.""" print 'Grid2Deff.timing2: reference CPU time = %g' % best_time dx = 1.0 / n g = Grid2Deff(dx=dx, dy=dx) # here we use straight NumPy sin in a scalar context: def myfunc1(x, y): return sin(x * y) + 8 * x def myfunc2(x, y): return math.sin(x * y) + 8 * x expression1 = StringFunction('sin(x*y) + 8*x', independent_variables=('x', 'y'), globals=globals()) expression1_f = expression1.__call__ # for efficiency and F77 callback expression2 = StringFunction('math.sin(x*y) + 8*x', independent_variables=('x', 'y'), globals=globals()) expression2_f = expression2.__call__ # for efficiency and F77 callback from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('Grid2Deff tests, %dx%d grid' % (n, n), best_time) t0a = timer(g.gridloop, (myfunc1, ), repetitions=1) e.add('g.gridloop, myfunc1', t0a) t0b = timer(g.gridloop, (myfunc2, ), repetitions=1) e.add('g.gridloop, myfunc2', t0b) t0c = timer(g.__call__, (myfunc1, ), repetitions=1) e.add('g.__call__, myfunc1', t0c) t0d = timer(g.__call__, (expression1_f, ), repetitions=1) e.add('g.__call__, expression1_f', t0d) t0e = timer(g.gridloop_itemset, (myfunc2, ), repetitions=1) e.add('g.gridloop_itemset, myfunc2', t0e) t1a = timer(g.ext_gridloop1, (myfunc1, ), repetitions=1) e.add('g.ext_gridloop1, myfunc1', t1a) t1b = timer(g.ext_gridloop1, (myfunc2, ), repetitions=1) e.add('g.ext_gridloop1, myfunc2', t1b) t2a = timer(g.ext_gridloop2, (myfunc1, ), repetitions=1) e.add('g.ext_gridloop2, myfunc1', t2a) t2b = timer(g.ext_gridloop2, (myfunc2, ), repetitions=1) e.add('g.ext_gridloop2, myfunc2', t2b) t3a = timer(g.ext_gridloop2, (expression1_f, ), repetitions=1) e.add('g.ext_gridloop2, expression1_f', t3a) t3b = timer(g.ext_gridloop2, (expression2_f, ), repetitions=1) e.add('g.ext_gridloop2, expression2_f', t3b) nrep = 20 # try the improved functions (works only for the F77 module): if 'gridloop_vec2' in dir(ext_gridloop): t4 = timer(g.ext_gridloop_vec2, (myfuncf2, ), repetitions=nrep) e.add('g.ext_gridloop_vec2, myfuncf2', t4) if 'gridloop2_str' in dir(ext_gridloop): t5 = timer(g.ext_gridloop2_str, ('myfunc', ), repetitions=nrep) e.add('g.ext_gridloop2_str, myfunc', t5) # try the version without allocation (first, make an a array): a = g.ext_gridloop2(myfunc1) # a has now Fortran storage t5b = timer(g.ext_gridloop_noalloc, ('myfunc', a), repetitions=nrep) e.add('g.ext_gridloop_noalloc, myfunc', t5b) # try 'inline' F77 compiled callback too: # (give F77 source for core of callback function as argument) g.ext_gridloop2_fcb_compile(str(expression1)) t6 = timer(g.ext_gridloop2_fcb, (), repetitions=nrep) e.add('g.ext_gridloop2_fcb(%s)' % repr(str(expression1)), t6) g.ext_gridloop2_fcb_ptr_compile(expression1) t6b = timer(g.ext_gridloop2_fcb_ptr, (), repetitions=nrep) e.add('g.ext_gridloop2_fcb_ptr(%s)' % repr(expression1), t6b) g.ext_gridloop2_compile(str(expression1)) t7 = timer(g.ext_gridloop2_v2, (), repetitions=nrep) e.add('g.ext_gridloop2_v2(%s)' % repr(str(expression1)), t7) # weave version: t8 = timer(g.ext_gridloop2_weave, (str(expression1), ), repetitions=nrep) e.add('g.ext_gridloop2_weave(%s)' % repr(str(expression1)), t8) # psyco: g.gridloop_psyco_init(g.gridloop) if g.gridloop_psyco != g.gridloop: # has psyco t9a = timer(g.gridloop_psyco, (myfunc2, ), repetitions=1) e.add('g.gridloop_psyco, myfunc2', t9a) t9b = timer(g.gridloop_psyco, (expression2_f, ), repetitions=1) e.add('g.gridloop_psyco, expression2_f', t9b) g.gridloop_psyco_init(g.gridloop_itemset) if g.gridloop_psyco != g.gridloop_itemset: # has psyco t9a = timer(g.gridloop_psyco, (myfunc2, ), repetitions=1) e.add('g.gridloop_psyco (itemset), myfunc2', t9a) t9b = timer(g.gridloop_psyco, (expression2_f, ), repetitions=1) e.add('g.gridloop_psyco (itemset), expression2_f', t9b) # instant: g.ext_gridloop1_instant(str(expression1)) if g.gridloop1_instant is not None: a = zeros((self.nx, self.ny)) t10 = timer(g.gridloop1_instant, (a, self.nx, g.ny, g.xcoor, g.ycoor), repetitions=nrep) e.add('g.gridloop1_instant', t10) print '\n\n\n\nrun from directory', os.getcwd() print e
def timing1(n=2000): # timing: dx = 1.0/n g = Grid2D(xmin=0, xmax=1, dx=dx, ymin=0, ymax=1, dy=dx) def myfunc(x, y): return sin(x*y) + 8*x from scitools.StringFunction import StringFunction expression = StringFunction('sin(x*y) + 8*x', independent_variables=('x','y'), globals=globals() # needed for vectorization ) from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('Grid2D efficiency tests, %dx%d grid' % (n,n)) t0 = time.clock() # vectorized expressions are so fast that we run the code # repeatedly rep=20 t1 = timer(g.__call__, (expression,), repetitions=rep, comment='StringFunction') e.add('g.__call__, expression (vectorized)', t1) t2 = timer(g.__call__, (myfunc,), repetitions=rep, comment='myfunc') e.add('g.__call__, myfunc (vectorized)', t2) f = g.gridloop_hardcoded_func() t3 = timer(g.gridloop_hardcoded_func, (), repetitions=1, comment='') e.add('g.gridloop_hardcoded_func', t3) t4 = timer(g.gridloop, (expression,), repetitions=1, comment='StringFunction') e.add('g.gridloop, expression (explicit loops)', t4) t5 = timer(g.gridloop, (myfunc,), repetitions=1, comment='myfunc') e.add('g.gridloop, myfunc (explicit loops)', t4) t6 = timer(g.gridloop_list, (expression,), repetitions=1, comment='StringFunction') e.add('g.gridloop_list, expression (explicit loops)', t6) t7 = timer(g.gridloop_list, (myfunc,), repetitions=1, comment='myfunc') e.add('g.gridloop_list, myfunc (explicit loops)', t7) from math import sin as mathsin # efficiency trick # The scalar computations above used sin from NumPy, which is # known to be slow for scalar arguments. Here we use math.sin # (stored in mathsin, could also use the slightly slower math.sin # explicitly) # taken globally so eval works: from math import sin as mathsin def myfunc_scalar(x, y): return mathsin(x*y) + 8*x expression_scalar = StringFunction('mathsin(x*y) + 8*x', independent_variables=('x','y'), globals=globals()) t8 = timer(g.gridloop, (expression_scalar,), repetitions=1, comment='eval(str)') e.add('g.gridloop, expression_scalar', t8) t9 = timer(g.gridloop, (myfunc_scalar,), repetitions=1, comment='myfunc') e.add('g.gridloop, myfunc_scalar', t9) print e
def timing1(n=2000): # timing: dx = 1.0 / n g = Grid2D(xmin=0, xmax=1, dx=dx, ymin=0, ymax=1, dy=dx) def myfunc(x, y): return sin(x * y) + 8 * x from scitools.StringFunction import StringFunction expression = StringFunction( 'sin(x*y) + 8*x', independent_variables=('x', 'y'), globals=globals() # needed for vectorization ) from scitools.misc import timer from scitools.EfficiencyTable import EfficiencyTable e = EfficiencyTable('Grid2D efficiency tests, %dx%d grid' % (n, n)) t0 = time.clock() # vectorized expressions are so fast that we run the code # repeatedly rep = 20 t1 = timer(g.__call__, (expression, ), repetitions=rep, comment='StringFunction') e.add('g.__call__, expression (vectorized)', t1) t2 = timer(g.__call__, (myfunc, ), repetitions=rep, comment='myfunc') e.add('g.__call__, myfunc (vectorized)', t2) f = g.gridloop_hardcoded_func() t3 = timer(g.gridloop_hardcoded_func, (), repetitions=1, comment='') e.add('g.gridloop_hardcoded_func', t3) t4 = timer(g.gridloop, (expression, ), repetitions=1, comment='StringFunction') e.add('g.gridloop, expression (explicit loops)', t4) t5 = timer(g.gridloop, (myfunc, ), repetitions=1, comment='myfunc') e.add('g.gridloop, myfunc (explicit loops)', t4) t6 = timer(g.gridloop_list, (expression, ), repetitions=1, comment='StringFunction') e.add('g.gridloop_list, expression (explicit loops)', t6) t7 = timer(g.gridloop_list, (myfunc, ), repetitions=1, comment='myfunc') e.add('g.gridloop_list, myfunc (explicit loops)', t7) from math import sin as mathsin # efficiency trick # The scalar computations above used sin from NumPy, which is # known to be slow for scalar arguments. Here we use math.sin # (stored in mathsin, could also use the slightly slower math.sin # explicitly) # taken globally so eval works: from math import sin as mathsin def myfunc_scalar(x, y): return mathsin(x * y) + 8 * x expression_scalar = StringFunction('mathsin(x*y) + 8*x', independent_variables=('x', 'y'), globals=globals()) t8 = timer(g.gridloop, (expression_scalar, ), repetitions=1, comment='eval(str)') e.add('g.gridloop, expression_scalar', t8) t9 = timer(g.gridloop, (myfunc_scalar, ), repetitions=1, comment='myfunc') e.add('g.gridloop, myfunc_scalar', t9) print e