[Tutor] how to speed up this code?

Pujo Aji ajikoe at gmail.com
Thu Oct 13 08:41:15 CEST 2005

I have code like this:
def f(x,y):
return math.sin(x*y) + 8 * x

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]

if __name__ == '__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 13x 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]);


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


Can anybody help?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051013/0cab59a3/attachment.html

More information about the Tutor mailing list