[yt-users] h5py execution time

John Wise jwise at astro.princeton.edu
Fri Aug 13 15:49:04 PDT 2010


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

http://www.dfanning.com/code_tips/slowloops.html

John

On 08/13/10 18:49, Jean-Claude Passy wrote:
> Hi Stephen,
>
> thanks, this was obvious but I did not think about it.
> Please enlarge my knowledge: why did "my method" with h5py require much
> more time than yours using Numpy ?
>
> Thanks,
>
> JC
>
> On 12/08/10 19:03, Stephen Skory wrote:
>> JC,
>>
>>> for k1 in range(0,size[0],1):
>>> for k2 in range(0,size[1],1):
>>> for k3 in range(0,size[2],1):
>>> if (density[k1,k2,k3]> Threshold):
>>> tmp[k1,k2,k3] = SomeValue
>>> else:
>>> tmp[k1,k2,k3] = 0.0
>>
>> Using Numpy array functions will make your life much better! Here is a
>> simple
>> example below that I think is roughly what you want to do. It should
>> at least
>> get you started down the right path. Luckily, 'density' is already a
>> numpy array
>> when it gets read in, so you're good to go!
>>
>>
>> In [1]: import numpy as np
>>
>> In [2]: a = np.random.random((2,2,2))
>>
>> In [3]: a
>> Out[3]:
>> array([[[ 0.97104489, 0.25985399],
>> [ 0.02646919, 0.50027277]],
>>
>> [[ 0.21725962, 0.19100341],
>> [ 0.96593371, 0.65269835]]])
>>
>> In [4]: big = (a> 0.5)
>>
>> In [5]: small = (a<= 0.5)
>>
>> In [6]: b = big * 0.7 + small * 0
>>
>> In [7]: b
>> Out[7]:
>> array([[[ 0.7, 0. ],
>> [ 0. , 0.7]],
>>
>> [[ 0. , 0. ],
>> [ 0.7, 0.7]]])
>>
>>
>>
>>
>> _______________________________________________________
>> sskory at physics.ucsd.edu o__ Stephen Skory
>> http://physics.ucsd.edu/~sskory/ _.>/ _Graduate Student
>> ________________________________(_)_\(_)_______________
>> _______________________________________________
>> yt-users mailing list
>> yt-users at lists.spacepope.org
>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org



More information about the yt-users mailing list