how to make this code faster

Discussion in 'Python' started by ajikoe@gmail.com, Oct 13, 2005.

  1. Guest

    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, 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/(float)(n);
    ycoor = i/(float)n;
    }

    for (int i=0; i<n; i++){
    for (int j=0; j<n; j++){
    a[j] = f(xcoor, 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
     
    , Oct 13, 2005
    #1
    1. Advertising

  2. Robert Kern Guest

    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, ycoor[j]) # f(x,y) = sin(x*y) + 8*x
    >
    > print a[1000,1000]
    > pass
    >
    > if __name__ == '__main__':
    > main()


    Ufuncs are your friend:

    from scipy import *

    def f(x, y):
    return sin(x*y) + 8*x

    def main():
    n = 2000
    ycoor = linspace(0.0, 1.0, n)
    xcoor = transpose(atleast_2d(ycoor))

    a = f(xcoor, ycoor)
    print a[1000, 1000]

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Oct 13, 2005
    #2
    1. Advertising

  3. Guest

    It looks like you're using Numeric for your arrays, but you are then
    pulling sin from the math module and calculating one point at a time.
    Instead try using sin(whole array) where sin is a ufunc from the
    Numeric module. Also, it's usually not good practice to "import
    Numeric as *". Instead try import Numeric as N so it's clear which
    functions you are using.

    -- David
     
    , Oct 13, 2005
    #3
  4. Juho Schultz Guest

    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, 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...
    >


    I guess the double loop (4E6 rounds) makes your program so slow.

    I assume you are using numarray or numeric for this.
    The built-in array operations are a lot faster.
    Try using them instead. And function calls are not free either.
    Xcoor and ycoor are equal, so there is no need to generate them both.

    I guess the following would be a lot faster:

    def func():
    n = 2000
    a = numarray.zeros((n,n), "Float")
    coor = numarray.arange(0,1,1/float(n))

    for i in range(n):
    a[:,i] = numarray.sin(coor*coor) + 8*coor

    print a[1000,1000]
    pass
     
    Juho Schultz, Oct 13, 2005
    #4
  5. Guest

    hello,

    I found that scipy only works with python 2.3 or?

    I don't know if the logic is correct:
    1. loop inside loop uses a lot of resources
    2. Numeric or Numpy can make program faster
    3. It use kind of Array/Matrix analysis style
    4. We have to change our algorithms so that Numeric or Numpy can help
    us, Matrix style

    Best Regards,
    pujo
     
    , Oct 13, 2005
    #5
  6. <> wrote:

    > hello,
    >
    > I found that scipy only works with python 2.3 or?


    You can use Numeric instead of scipy if you need/want to:

    from Numeric import arange,reshape,sin

    def computeMatrix(n):
    xcoor = arange(0,1,1/float(n))
    ycoor = reshape(xcoor, (n,1))
    return sin(xcoor*ycoor) + 8*xcoor

    Note that arange() does not include the endpoint, i.e. arange(0,1,0.25).tolist() ==[0.0, 0.25, 0.5,
    0.75].

    > I don't know if the logic is correct:
    > 1. loop inside loop uses a lot of resources
    > 2. Numeric or Numpy can make program faster
    > 3. It use kind of Array/Matrix analysis style
    > 4. We have to change our algorithms so that Numeric or Numpy can help
    > us, Matrix style


    That's correct more or less.

    George
     
    George Sakkis, Oct 13, 2005
    #6
  7. Tony Nelson Guest

    In article <>,
    "" <> 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, 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@georgea*lastname*.com
    ' <http://www.georgeanelson.com/>
     
    Tony Nelson, Oct 15, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Python Maniac
    Replies:
    24
    Views:
    778
    Matt McCredie
    Sep 23, 2007
  2. Ruby Maniac
    Replies:
    57
    Views:
    597
    Chad Perrin
    Sep 27, 2007
  3. 罗晟

    how to make my ruby code faster?

    罗晟, Jan 5, 2012, in forum: Ruby
    Replies:
    1
    Views:
    711
    Robert Klemme
    Jan 5, 2012
  4. Replies:
    35
    Views:
    335
    Uri Guttman
    May 18, 2008
  5. Replies:
    18
    Views:
    174
    Michael Torrie
    Jul 21, 2013
Loading...

Share This Page