<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Thanks guys, that is very
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<span class="clickable" onclick='redirectWR(event,"OXfren")'>
enlightening</span>.<br>
<br>
Best,<br>
<br>
JC<br>
<br>
On 13/08/10 17:02, Stephen Skory wrote:
<blockquote cite="mid:511271.64820.qm@web112416.mail.gq1.yahoo.com"
type="cite">
<pre wrap="">JC,
</pre>
<blockquote type="cite">
<pre wrap="">In my experience, interpreted languages are slow with loops. If you use
the array syntax in numpy, all of the loops are done in C. The same
thing happens in IDL, as well, where you dearly try to avoid loops.
This page helped me long ago in IDL...
</pre>
</blockquote>
<pre wrap="">
John is right about interpreted languages. To expand on the point about Numpy
and C, if you have a Numpy array (which is what h5py outputs when you read data
out of a file), and you operate on it using Numpy functions, the work is done in
compiled C code. This means it's much faster. Here is some example code that
does the same thing twice, once with a Python loop and the other in Numpy:
----------
import time, math
import numpy as np
a = np.random.random(1e7).astype('float64')
b = np.empty(1e7, dtype='float64')
c = np.empty(1e7, dtype='float64')
t0 = time.time()
for i, item in enumerate(a):
b[i] = math.pow(item, 2.)
t1 = time.time()
print 'Python loop takes %f seconds' % (t1-t0)
t0 = time.time()
np.power(a, 2., c)
t1 = time.time()
print 'Numpy C loop takes %f seconds' % (t1-t0)
print 'The arrays are the same?', (b == c).all()
-----------
Noting that I had to enforce 64-bit so the answers will match up, I get this
when I run it:
python timeme.py
Python loop takes 9.354742 seconds
Numpy C loop takes 0.584433 seconds
The arrays are the same? True
I think the advantages are obvious! You had a triple loop in your original code,
so the effect is likely even worse than this.
I hope this helps!
_______________________________________________________
<a class="moz-txt-link-abbreviated" href="mailto:sskory@physics.ucsd.edu">sskory@physics.ucsd.edu</a> o__ Stephen Skory
<a class="moz-txt-link-freetext" href="http://physics.ucsd.edu/~sskory/">http://physics.ucsd.edu/~sskory/</a> _.>/ _Graduate Student
________________________________(_)_\(_)_______________
_______________________________________________
yt-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:yt-users@lists.spacepope.org">yt-users@lists.spacepope.org</a>
<a class="moz-txt-link-freetext" href="http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org">http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org</a>
</pre>
</blockquote>
</body>
</html>