()
Run some benchmarks for clambdify and frange.

    NumPy and Psyco are used as reference if available.
Run some benchmarks for clambdify and frange.
Run some benchmarks for clambdify and frange .
def benchmark():
    """
    Run some benchmarks for clambdify and frange.

    NumPy and Psyco are used as reference if available.
    """
    from time import time
    from timeit import Timer

    def fbenchmark(f, var=[Symbol('x')]):
        """
        Do some benchmarks with f using clambdify, lambdify and psyco.
        """
        global cf, pf, psyf
        start = time()
        cf = clambdify(var, f)
        print('compile time (including sympy overhead): %f s' % (
            time() - start))
        pf = lambdify(var, f, 'math')
        psyf = None
        psyco = import_module('psyco')
        if psyco:
            psyf = lambdify(var, f, 'math')
            psyco.bind(psyf)
        code = '''for x in (i/1000. for i in range(1000)):
        f(%s)''' % ('x,'*len(var)).rstrip(',')
        t1 = Timer(code, 'from __main__ import cf as f')
        t2 = Timer(code, 'from __main__ import pf as f')
        if psyf:
            t3 = Timer(code, 'from __main__ import psyf as f')
        else:
            t3 = None
        print('for x = (0, 1, 2, ..., 999)/1000')
        print('20 times in 3 runs')
        print('compiled:      %.4f %.4f %.4f' % tuple(t1.repeat(3, 20)))
        print('Python lambda: %.4f %.4f %.4f' % tuple(t2.repeat(3, 20)))
        if t3:
            print('Psyco lambda:  %.4f %.4f %.4f' % tuple(t3.repeat(3, 20)))

    print('big function:')
    from sympy import _exp, _sin, _cos, pi, lambdify
    x = Symbol('x')
##    f1 = diff(_exp(x)**2 - _sin(x)**pi, x) \
##        * x**12-2*x**3+2*_exp(x**2)-3*x**7+4*_exp(123+x-x**5+2*x**4) \
##        * ((x + pi)**5).expand()
    f1 = 2*_exp(x**2) + x**12*(-pi*_sin(x)**((-1) + pi)*_cos(x) + 2*_exp(2*x)) \
        + 4*(10*pi**3*x**2 + 10*pi**2*x**3 + 5*pi*x**4 + 5*x*pi**4 + pi**5
        + x**5)*_exp(123 + x + 2*x**4 - x**5) - 2*x**3 - 3*x**7
    fbenchmark(f1)
    print()
    print('simple function:')
    y = Symbol('y')
    f2 = sqrt(x*y) + x*5
    fbenchmark(f2, [x, y])
    times = 100000
    fstr = '_exp(_sin(_exp(-x**2)) + sqrt(pi)*_cos(x**5/(x**3-x**2+pi*x)))'
    print
    print('frange with f(x) =')
    print(fstr)
    print('for x=1, ..., %i' % times)
    print('in 3 runs including full compile time')
    t4 = Timer("frange('lambda x: %s', 0, %i)" % (fstr, times),
               'from __main__ import frange')

    numpy = import_module('numpy')

    print('frange:        %.4f %.4f %.4f' % tuple(t4.repeat(3, 1)))
    if numpy:
        t5 = Timer('x = arange(%i); result = %s' % (times, fstr),
                   'from numpy import arange, sqrt, exp, sin, cos, exp, pi')
        print('numpy:         %.4f %.4f %.4f' % tuple(t5.repeat(3, 1)))
def benchmark ( ) : from time import time from timeit import Timer def fbenchmark ( f , var = [ Symbol ( 'x' ) ] ) : """
        Do some benchmarks with f using clambdify, lambdify and psyco.
        """ global cf , pf , psyf start = time ( ) cf = clambdify ( var , f ) print ( 'compile time (including sympy overhead): %f s' % ( time ( ) - start ) ) pf = lambdify ( var , f , 'math' ) psyf = None psyco = import_module ( 'psyco' ) if psyco : psyf = lambdify ( var , f , 'math' ) psyco . bind ( psyf ) code = '''for x in (i/1000. for i in range(1000)):
        f(%s)''' % ( 'x,' * len ( var ) ) . rstrip ( ',' ) t1 = Timer ( code , 'from __main__ import cf as f' ) t2 = Timer ( code , 'from __main__ import pf as f' ) if psyf : t3 = Timer ( code , 'from __main__ import psyf as f' ) else : t3 = None print ( 'for x = (0, 1, 2, ..., 999)/1000' ) print ( '20 times in 3 runs' ) print ( 'compiled:      %.4f %.4f %.4f' % tuple ( t1 . repeat ( 3 , 20 ) ) ) print ( 'Python lambda: %.4f %.4f %.4f' % tuple ( t2 . repeat ( 3 , 20 ) ) ) if t3 : print ( 'Psyco lambda:  %.4f %.4f %.4f' % tuple ( t3 . repeat ( 3 , 20 ) ) ) print ( 'big function:' ) from sympy import _exp , _sin , _cos , pi , lambdify x = Symbol ( 'x' ) ##    f1 = diff(_exp(x)**2 - _sin(x)**pi, x) \ ##        * x**12-2*x**3+2*_exp(x**2)-3*x**7+4*_exp(123+x-x**5+2*x**4) \ ##        * ((x + pi)**5).expand() f1 = 2 * _exp ( x ** 2 ) + x ** 12 * ( - pi * _sin ( x ) ** ( ( - 1 ) + pi ) * _cos ( x ) + 2 * _exp ( 2 * x ) ) + 4 * ( 10 * pi ** 3 * x ** 2 + 10 * pi ** 2 * x ** 3 + 5 * pi * x ** 4 + 5 * x * pi ** 4 + pi ** 5 + x ** 5 ) * _exp ( 123 + x + 2 * x ** 4 - x ** 5 ) - 2 * x ** 3 - 3 * x ** 7 fbenchmark ( f1 ) print ( ) print ( 'simple function:' ) y = Symbol ( 'y' ) f2 = sqrt ( x * y ) + x * 5 fbenchmark ( f2 , [ x , y ] ) times = 100000 fstr = '_exp(_sin(_exp(-x**2)) + sqrt(pi)*_cos(x**5/(x**3-x**2+pi*x)))' print print ( 'frange with f(x) =' ) print ( fstr ) print ( 'for x=1, ..., %i' % times ) print ( 'in 3 runs including full compile time' ) t4 = Timer ( "frange('lambda x: %s', 0, %i)" % ( fstr , times ) , 'from __main__ import frange' ) numpy = import_module ( 'numpy' ) print ( 'frange:        %.4f %.4f %.4f' % tuple ( t4 . repeat ( 3 , 1 ) ) ) if numpy : t5 = Timer ( 'x = arange(%i); result = %s' % ( times , fstr ) , 'from numpy import arange, sqrt, exp, sin, cos, exp, pi' ) print ( 'numpy:         %.4f %.4f %.4f' % tuple ( t5 . repeat ( 3 , 1 ) ) )