how to make this code faster
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

how to make this code faster

From: <ajikoe@gmail.com>
Date: Thu Oct 13 2005 - 08:08:37 CEST

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 try to make this run faster even using psyco, but I found this still
slow, I tried using java and found it around 8x faster...

public class s1 {

        /**
         * @param args
         */
        public static int n = 2000;
        public static double[][] a = new double[n][n];
        public static double [] xcoor = new double[n];
        public static double [] ycoor = new double[n];

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                for (int i=0; i<n; i++){
                        xcoor[i] = i/(float)(n);
                        ycoor[i] = i/(float)n;
                }

                for (int i=0; i<n; i++){
                        for (int j=0; j<n; j++){
                                a[i][j] = f(xcoor[i], ycoor[j]);
                        }
                }

                System.out.println(a[1000][1000]);

        }
        public static double f(double x, double y){
                return Math.sin(x*y) + 8*x;
        }

}

Can anybody help?

pujo
Received on Sat Oct 15 04:30:49 2005