It's way past my bedtime, but any use of numpy/numarray that involves
two nested for loops to step over each element is the wrong solution
:)  You need to figure out how to get rid of that inner for. 
That is what is slowing you down.&nbsp; <br>
<br>
Compare these two ways to multiply a 1000 element array by 100.&nbsp;
The first one steps over the elements one at a time, multiplying each
one in turn.&nbsp; The second multiplies the entire array at
once.&nbsp; Which boils down to looping over 2000 rows, instead of
4,000,000 elements :)<br>
<br>
If I was more awake, I'd try to figure out how you can do that.&nbsp;
But this should give you an idea of what arrays are useful for, and how
to approach the problem.<br>
<br>
&gt;&gt;&gt; def time_loop(num):<br>
... &nbsp;&nbsp;&nbsp; a = arange(1000)<br>
... &nbsp;&nbsp;&nbsp; b = zeros(1000)<br>
... &nbsp;&nbsp;&nbsp; t = time.clock()<br>
... &nbsp;&nbsp;&nbsp; for i in range(num):<br>
... &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for i in range(len(a)):<br>
... &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; b[i] = a[i] * 100.0<br>
... &nbsp;&nbsp;&nbsp; print time.clock() - t<br>
... &nbsp;&nbsp;&nbsp; <br>
&gt;&gt;&gt; time_loop(100000)<br>
59.7517100637<br>
<br>
<br>
&gt;&gt;&gt; def time_numeric(num):<br>
... &nbsp;&nbsp;&nbsp; a = arange(1000)<br>
... &nbsp;&nbsp;&nbsp; b = zeros(1000)<br>
... &nbsp;&nbsp;&nbsp; t = time.clock()<br>
... &nbsp;&nbsp;&nbsp; for i in range(num):<br>
... &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; b = a*100<br>
... &nbsp;&nbsp;&nbsp; print time.clock() - t<br>
... &nbsp;&nbsp;&nbsp; <br>
&gt;&gt;&gt; time_numeric(100000)<br>
1.44588097091<br><br>
<br>
<br><div><span class="gmail_quote">On 10/13/05, <b class="gmail_sendername">Pujo Aji</b> &lt;<a href="mailto:ajikoe@gmail.com">ajikoe@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><a name="106e8b63d5698edf_msg_d6f84fd07a81ecf0"></a><font face="Courier, Monospaced"><font face="Courier New" size="2">I have code like this: <br></font></font></div>
<div><font face="Courier, Monospaced"><font face="Courier New" size="2">def f(x,y): <br>&nbsp; &nbsp; return math.sin(x*y) + 8 * x <br>&nbsp;</font></font></div>
<p>def main(): <br>&nbsp; &nbsp; n = 2000 <br>&nbsp; &nbsp; a = zeros((n,n), Float) <br>&nbsp; &nbsp; xcoor = arange(0,1,1/float(n)) <br>&nbsp; &nbsp; ycoor = arange(0,1,1/float(n)) <br>
</p><p>&nbsp; &nbsp; for i in range(n): <br>&nbsp; &nbsp; &nbsp; &nbsp; for j in range(n): <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i,j] = f(xcoor[i], ycoor[j]) &nbsp;# f(x,y) = sin(x*y) + 8*x <br>
</p><p>&nbsp; &nbsp; print a[1000,1000] <br>&nbsp; &nbsp; pass <br>
</p><p>if __name__ == '__main__': <br>&nbsp; &nbsp; main() <br>
</p><p>I try to make this run faster even using psyco, but I found this still <br>slow, I tried using java and found it around&nbsp;13x faster... <br>
</p><p>public class s1 { <br>
</p><p>&nbsp; &nbsp; &nbsp; &nbsp; /** <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param args <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/ <br>&nbsp; &nbsp; &nbsp; &nbsp; public static int n = 2000; <br>&nbsp; &nbsp; &nbsp; &nbsp; public static double[][] a = new double[n][n]; <br>&nbsp; &nbsp; &nbsp; &nbsp; public static double [] xcoor = new double[n]; 
<br>
&nbsp; &nbsp; &nbsp; &nbsp; public static double [] ycoor = new double[n]; <br>
</p><p>&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) { <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i&lt;n; i++){ <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xcoor[i] = i/(float)(n); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ycoor[i] = i/(float)n; 
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br>
</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i&lt;n; i++){ <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j=0; j&lt;n; j++){ <br>&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i][j] = f(xcoor[i], ycoor[j]); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } 
<br>
</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a[1000][1000]); <br>
</p><p>&nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp; &nbsp; &nbsp; &nbsp; public static double f(double x, double y){ <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Math.sin(x*y) + 8*x; <br>&nbsp; &nbsp; &nbsp; &nbsp; } <br>
</p><p>
</p><div>} <br><br>&nbsp;</div>Can anybody help? <br>
<p>pujo <br></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p>

<br>_______________________________________________<br>Tutor maillist &nbsp;- &nbsp;<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:Tutor@python.org">Tutor@python.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">
http://mail.python.org/mailman/listinfo/tutor</a><br><br><br></blockquote></div><br>