Velocity Reviews > how to make this code faster

# how to make this code faster

ajikoe@gmail.com
Guest
Posts: n/a

 10-13-2005
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

Robert Kern
Guest
Posts: n/a

 10-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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()

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
(E-Mail Removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

davbrow@gmail.com
Guest
Posts: n/a

 10-13-2005
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

Juho Schultz
Guest
Posts: n/a

 10-13-2005
(E-Mail Removed) 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 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[i]) + 8*coor

print a[1000,1000]
pass

ajikoe@gmail.com
Guest
Posts: n/a

 10-13-2005
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

George Sakkis
Guest
Posts: n/a

 10-13-2005
<(E-Mail Removed)> 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

Tony Nelson
Guest
Posts: n/a

 10-15-2005
In article <(E-Mail Removed) .com>,
"(E-Mail Removed)" <(E-Mail Removed)> 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@georgea*lastname*.com
' <http://www.georgeanelson.com/>