how to make this code faster

Tony Nelson *firstname*nlsnews at georgea*lastname*.com
Sat Oct 15 01:16:00 EDT 2005


In article <1129183717.183243.100540 at g49g2000cwa.googlegroups.com>,
 "ajikoe at gmail.com" <ajikoe at gmail.com> wrote:

> def f(x,y):
>     return math.sin(x*y) + 8 * x
> I have code like this:
> 
> def main():
>     n = 2000
>     a = zeros((n,n), Float)
>     xcoor = arange(0,1,1/float(n))
>     ycoor = arange(0,1,1/float(n))
> 
> 
>     for i in range(n):
>         for j in range(n):
>             a[i,j] = f(xcoor[i], ycoor[j])  # f(x,y) = sin(x*y) + 8*x
> 
>     print a[1000,1000]
>     pass
> 
> if __name__ == '__main__':
>     main()

I would guess that you are spending most of your time calculating 
sin(x*y).  To find out, just replace f(x,y) with 1, which will produce 
wrong results really fast, and see what that does to your execution time.
________________________________________________________________________
TonyN.:'                        *firstname*nlsnews at georgea*lastname*.com
      '                                  <http://www.georgeanelson.com/>



More information about the Python-list mailing list