[SciPy-User] Speed-up simple function

Nicolau Werneck nwerneck at gmail.com
Mon Jan 10 11:14:46 EST 2011


Hi. I tried this function in my Atom N450 computer, with two vectors
with 100.000 numbers, and it took only 90ms. Do you really need it
faster than this? Do you have to make these calculations to a lot of
different 100k long vectors, or something like that?...

If you really need to speed that up, you just have to implement this
_dens0 function using weave, Cython or something like that. I can give
you some more info about Cython, write me at my address if you want.

++nic



On Mon, Jan 10, 2011 at 04:47:44PM +0100, g.plantageneto at runbox.com wrote:
> 
> > >
> > This looks terribly over complicated. What are you trying to do and where is
> > the polynomial?
> > 
> 
> I see, sorry, I'll try to put it more clearly. My computation is something like this: for each time step, I compute the value of some polynomial on all array elements, then I average the results to obtain a single number:
> 
> ###############################################
> 
> for tstep in xrange(0,end):
>        VAR1     = Read from some NetCDF file (~10^5 elements)
>        VAR2     = Read from some NetCDF file (~10^5 elements)
>        COMP1  = FUNC1(VAR1,VAR2)
>        COMP2  = FUNC2(VAR1,COMP1)
>        RESULT[tstep] = np.average(COMP2,weights=W)
> 
> ###############################################
> 
> I checked and the bottleneck is really in the computations done by FUNC1 and FUNC2.
> The polynomials are in the functions FUNC1 and FUNC2 (from python seawater library, a library that provides some standard functions for seawater properties). As an example (they are all rather similar, just polynomials), one of the functions is:
> 
> ###########################################################
> 
> def _dens0(S,T):
>     """Density of seawater at zero pressure"""
> 
>     # --- Define constants ---
>     a0 = 999.842594
>     a1 =   6.793952e-2
>     a2 =  -9.095290e-3
>     a3 =   1.001685e-4
>     a4 =  -1.120083e-6
>     a5 =   6.536332e-9
> 
>     b0 =   8.24493e-1
>     b1 =  -4.0899e-3
>     b2 =   7.6438e-5
>     b3 =  -8.2467e-7
>     b4 =   5.3875e-9
> 
>     c0 =  -5.72466e-3
>     c1 =   1.0227e-4
>     c2 =  -1.6546e-6
> 
>     d0 =   4.8314e-4
> 
>     # --- Computations ---
>     # Density of pure water
>     SMOW = a0 + (a1 + (a2 + (a3 + (a4 + a5*T)*T)*T)*T)*T
> 
>     # More temperature polynomials
>     RB = b0 + (b1 + (b2 + (b3 + b4*T)*T)*T)*T
>     RC = c0 + (c1 + c2*T)*T
> 
>     return SMOW + RB*S + RC*(S**1.5) + d0*S*S
> 
> ####################################################
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

-- 
Nicolau Werneck <nwerneck at gmail.com>          C3CF E29F 5350 5DAA 3705
http://www.lti.pcs.usp.br/~nwerneck           7B9E D6C4 37BB DA64 6F15
Linux user #460716
"A language that doesn't affect the way you think about programming, is not worth knowing."
-- Alan J. Perlis




More information about the SciPy-User mailing list