By "pure python" I'm assuming you mean part of the stdlib.
Referring to
https://wiki.python.org/moin/PythonSpeed/PerformanceTips
you could end with something like this (untested).
def getWaveData(diskBuffer):
offset = 0
words = array('H')
wx = words.extend #saves two lookups and a function call
su = struct.unpack_from #saves two lookups
# 'i' not used in the loop so throw it away
for _ in range(len(diskBuffer)/3): # use xrange on Python 2
h0 = su('>h',diskBuffer,offset)
h1 = su('<h',diskBuffer,offset+1)
wx((h0[0] & 0xfff0), (h1[0] & 0xfff0)) # MRAB pointed out a
problem with the masking in the second section???
offset += 3
return words
> I thought of unpacking more bytes at once e.g. using a format '>hxhxhxhx' for 4 even samples
> and '<xhxhxhxh' for 4 odd samples vice versa.
If that reduces the number of times around the loop why not? Combine it
with MRAB's suggestion of lookups and I'd guess you'd get a speedup, but
knowing Python I'm probably way out on that? There's only one way to
find out.
I'm also thinking that you could user one of the itertools functions or
recipes to grab the data and hence simplify the loop even more, but it's
now 3:45 BST, so I can't think straight, hence bed.
> Can I map the '& 0xfff0' to the whole array?
If it works :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence