[SciPy-User] Speed-up simple function

J. David Lee johnl at cs.wisc.edu
Mon Jan 10 09:25:48 EST 2011


Andrea,

Here is an example of scipy.weave that I generally use as a starting point:

from scipy.weave import inline
from scipy.weave.converters import blitz
from pylab import *

def foo(arr, c):
     ret = arr.copy()
     cvars = ['ret', 'c']
     # Using blitz converter gives the following for an array arr:
     #    Narr[0] is length of first dimension
     #    Narr[1] is length of second dimension
     #    arr(a, b) is the element arr[a][b]
     code = """
         int i = Nret[0];
         while(i--) {
             ret(i) += c;
         }
     """
     inline(code, cvars, type_converters = blitz)
     return ret

a = arange(5)
foo(a, 1)
print a

I generally create all my python objects outside the code so I don't 
screw up any reference counting.  One caveat is that you need to run the 
code in a single process the first time around or you can end up with 
some strange errors.

David

On 01/10/2011 07:59 AM, g.plantageneto at runbox.com wrote:
> Hi everybody,
>
> I have some functions in python that perform simple computations (they compute the values of some long polynomials). Since I apply them to rather large arrays (10^5 elements) these functions slow down the script quite a bit. Is there a quick and simple way to speed up these functions?
>
> Thanks,
> Andrea
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user




More information about the SciPy-User mailing list