I'm kind of new to Python and I have to implement "fast as possible" version of this code.
s="<%dH" % (int(width*height),)
z=struct.unpack(s, contents)
heights = np.zeros((height,width))
for r in range(0,height):
for c in range(0,width):
elevation=z[((width)*r)+c]
if (elevation==65535 or elevation<0 or elevation>20000):
elevation=0.0
heights[r][c]=float(elevation)
I've read some of the python vectorization questions... but I don't think it applies to my case. Most of the questions are things like using np.sum instead of for loops. I guess I have two questions:
- Is it possible to speed up this code...I think
heights[r][c]=float(elevation)is where the bottleneck is. I need to find some Python timing commands to confirm this. - If it possible to speed up this code. What are my options? I have seen some people recommend
cython,pypy,weave. I could do this faster in C but this code also need to generate plots so I'd like to stick with Python so I can usematplotlib.
dostuff()intoprofile.runctx('dostuff()', globals(), locals(), filename='out.profile')