Efficient coding advice?

Johann Hibschman johann at physics.berkeley.edu
Tue Apr 25 18:09:09 CEST 2000


Kelvin Chu writes:

> data = fp.readframes(nframes)
> left = Numeric.zeros(nframes)
> right = Numeric.zeros(nframes)
> for i in range(0,nframes):
>     left[i] = ord(data[i*2])
>     right[i] = ord(data[i*2+1])

Try:

ords = map(ord, data)            # take the ord of all the chars
ords.shape = (nframes/2, 2)      # reshape the array in place
left  = ords[:,0]                # pick out individual columns
right = ords[:,1]

That should be durn fast, as long as "readframes" isn't the bottleneck.

-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list