how to make this code faster

A

ajikoe

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
 
R

Robert Kern

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
(e-mail address removed)

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

davbrow

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
 
J

Juho Schultz

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
 
A

ajikoe

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
 
G

George Sakkis

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
 
T

Tony Nelson

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/>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top