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. <br>
<br>
Compare these two ways to multiply a 1000 element array by 100.
The first one steps over the elements one at a time, multiplying each
one in turn. The second multiplies the entire array at
once. 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.
But this should give you an idea of what arrays are useful for, and how
to approach the problem.<br>
<br>
>>> def time_loop(num):<br>
... a = arange(1000)<br>
... b = zeros(1000)<br>
... t = time.clock()<br>
... for i in range(num):<br>
... for i in range(len(a)):<br>
... b[i] = a[i] * 100.0<br>
... print time.clock() - t<br>
... <br>
>>> time_loop(100000)<br>
59.7517100637<br>
<br>
<br>
>>> def time_numeric(num):<br>
... a = arange(1000)<br>
... b = zeros(1000)<br>
... t = time.clock()<br>
... for i in range(num):<br>
... b = a*100<br>
... print time.clock() - t<br>
... <br>
>>> 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> <<a href="mailto:ajikoe@gmail.com">ajikoe@gmail.com</a>> 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> return math.sin(x*y) + 8 * x <br> </font></font></div>
<p>def main(): <br> n = 2000 <br> a = zeros((n,n), Float) <br> xcoor = arange(0,1,1/float(n)) <br> ycoor = arange(0,1,1/float(n)) <br>
</p><p> for i in range(n): <br> for j in range(n): <br> a[i,j] = f(xcoor[i], ycoor[j]) # f(x,y) = sin(x*y) + 8*x <br>
</p><p> print a[1000,1000] <br> pass <br>
</p><p>if __name__ == '__main__': <br> 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 13x faster... <br>
</p><p>public class s1 { <br>
</p><p> /** <br> * @param args <br> */ <br> public static int n = 2000; <br> public static double[][] a = new double[n][n]; <br> public static double [] xcoor = new double[n];
<br>
public static double [] ycoor = new double[n]; <br>
</p><p> public static void main(String[] args) { <br> // TODO Auto-generated method stub <br> for (int i=0; i<n; i++){ <br> xcoor[i] = i/(float)(n); <br> ycoor[i] = i/(float)n;
<br> } <br>
</p><p> for (int i=0; i<n; i++){ <br> for (int j=0; j<n; j++){ <br>
a[i][j] = f(xcoor[i], ycoor[j]); <br> } <br> }
<br>
</p><p> System.out.println(a[1000][1000]); <br>
</p><p> } <br> public static double f(double x, double y){ <br> return Math.sin(x*y) + 8*x; <br> } <br>
</p><p>
</p><div>} <br><br> </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 - <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>