Skip to content

Instantly share code, notes, and snippets.

@tboggs
Created May 22, 2014 01:54
Show Gist options
  • Save tboggs/561e42f066c26476acc3 to your computer and use it in GitHub Desktop.
Save tboggs/561e42f066c26476acc3 to your computer and use it in GitHub Desktop.
A timing script for SPy SpyFile read methods with and without memmaps
# Timing tests for SpyFile read_ methods
from spectral import *
def create_files(src='av920612_NS_line.lan'):
img = open_image(src)
envi.save_image('bilfile.hdr', img, interleave='bil')
envi.save_image('bipfile.hdr', img, interleave='bip')
envi.save_image('bsqfile.hdr', img, interleave='bsq')
def timed(func):
'''A timer for FileTimer read methods.'''
import time
def wrapped(*args):
# Replace file name with associated SpyFile object
args = [args[0]] + [open_image(args[1])] + list(args[2:])
t = time.time()
func(*args)
return time.time() - t
return wrapped
class FileTimer:
@timed
def read_pixel(self, img, use_memmap):
img.read_pixel(100, 100, use_memmap)
@timed
def read_band(self, img, use_memmap):
img.read_band(100, use_memmap)
@timed
def read_bands(self, img, use_memmap):
img.read_bands([20, 30, 150], use_memmap)
@timed
def read_subregion(self, img, use_memmap):
img.read_subregion((40, 150), (30, 200), None, use_memmap)
@timed
def read_subimage(self, img, use_memmap):
img.read_subimage(range(200, 400, 3), range(300, 500, 2), None, use_memmap)
@timed
def read_datum(self, img, use_memmap):
img.read_datum(30, 300, 150, use_memmap)
def run(self):
import os
if not os.path.isfile('bilfile.hdr'):
print 'Creating interleave files...'
create_files()
images = ['%sfile.hdr' % s for s in ['bil', 'bip', 'bsq']]
names = ['band', 'bands', 'pixel', 'subregion', 'subimage', 'datum']
tests = [self.read_band, self.read_bands, self.read_pixel,
self.read_subregion, self.read_subimage, self.read_datum]
print 'Speedups using memmap in read methods:\n'
print '{:15} {:^10} {:^10} {:^10}'.format('method', 'BIL', 'BIP', 'BSQ')
print '-' * 48
for (n, t) in zip(names, tests):
speedups = [t(img, False) / t(img, True) for img in images]
# speedups = [1. / (t(img, True) / t(img, False)) for img in images]
print '{0:15} {1[0]:^10.2f} {1[1]:^10.2f} {1[2]:^10.2f}'.format('read_' + n, speedups)
if __name__ == '__main__':
timer = FileTimer()
timer.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment