Skip to content

Commit eec5efe

Browse files
author
ARF
committed
__shapeIndex optimization: use memoryview slicing to skip unused field
x2.0 speedup over previous commit
1 parent 4c0b9b4 commit eec5efe

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

shapefile.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import tempfile
1919
import itertools
2020

21+
try:
22+
memoryview(b'')
23+
except NameError:
24+
memoryview = lambda x: x
25+
2126
#
2227
# Constants for shape types
2328
NULL = 0
@@ -389,10 +394,12 @@ def __shapeIndex(self, i=None):
389394
numRecords = shxRecordLength // 8
390395
# Jump to the first record.
391396
shx.seek(100)
392-
shxRecords = array.array('i', unpack(">" + "i4x" * numRecords,
393-
shx.read((4+4) * numRecords)))
397+
shxRecords = array.array('i')
398+
shxRecords.fromfile(shx, 2 * numRecords)
399+
if sys.byteorder != 'big':
400+
shxRecords.byteswap()
394401
# Offsets are 16-bit words just like the file length
395-
self._offsets = [2*el for el in shxRecords]
402+
self._offsets = [2 * el for el in memoryview(shxRecords)[::2]]
396403
if not i == None:
397404
return self._offsets[i]
398405

0 commit comments

Comments
 (0)