riggsd / zcant

Myotisoft ZCANT, a zero-crossing analysis tool for bat echolocation data
MIT License
3 stars 1 forks source link

Speed: WAV Reading Code #9

Closed riggsd closed 7 years ago

riggsd commented 7 years ago

Quick comparison of our zcant.conversion.read_wav() routine versus scipy.io.wavfile.read(). Shows that the latter is ~100 times faster, and can sometimes be ~600 times faster when using mmap. The difference isn't quite as great with smaller .WAV files, but amounts to a 0.5sec loadtime difference for a 10mb .WAV file.

Our implementation uses Struct.unpack_from(), which yields a Python list, and is in turn converted to an np.ndarray. Their implementation uses np.fromstring() or np.memmap() so the extraction is performed entirely in C.

Either switch to using scipy.io.wavfile.read() or utilize the more efficient C-based functions for loading wav data.

import timeit
import os.path

n = 1
size = float(os.path.getsize('test.wav')) / 1024 / 1024
print 'loading file "test.wav" (%.1f mb) %d times...' % (size, n)

print '\nzcant.conversion.load_wav()'
t = timeit.Timer('load_wav("test.wav")', 'from zcant.conversion import load_wav')
print '%.4f' % t.timeit(n)

print '\nscipy.io.wavfile.read()'
t = timeit.Timer('wavfile.read("test.wav")', 'from scipy.io import wavfile')
print '%.4f' % t.timeit(n)

print '\nscipy.io.wavfile.read(mmap=True)'
t = timeit.Timer('wavfile.read("test.wav", mmap=True)', 'from scipy.io import wavfile')
print '%.4f' % t.timeit(n)
loading file "test.wav" (11.0 mb) 1 times...

zcant.conversion.load_wav()
0.5974

scipy.io.wavfile.read()
0.0084

scipy.io.wavfile.read(mmap=True)
0.0015
riggsd commented 7 years ago
loading file "test.wav" (2.9 mb) 25 times...

zcant.conversion.load_wav()
3.1342

scipy.io.wavfile.read()
0.0339

scipy.io.wavfile.read(mmap=True)
0.0134