From e1b5d95b430fe7650c4e0a41d3e3cfca13d5b7ee Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Thu, 12 Dec 2019 00:50:12 -0800 Subject: [PATCH] use numpy for faster de/serialization --- gdsii/record.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/gdsii/record.py b/gdsii/record.py index 89a1978..311c0a8 100644 --- a/gdsii/record.py +++ b/gdsii/record.py @@ -27,6 +27,7 @@ from . import exceptions, tags, types from datetime import datetime import math import struct +import numpy __all__ = [ 'Record', @@ -76,7 +77,7 @@ def _parse_int2(data): data_len = len(data) if not data_len or (data_len % 2): raise exceptions.IncorrectDataSize('INT2') - return struct.unpack('>%dh' % (data_len//2), data) + return numpy.frombuffer(data, dtype='>i2', count=data_len // 2) def _parse_int4(data): """ @@ -96,7 +97,7 @@ def _parse_int4(data): data_len = len(data) if not data_len or (data_len % 4): raise exceptions.IncorrectDataSize('INT4') - return struct.unpack('>%dl' % (data_len//4), data) + return numpy.frombuffer(data, dtype='>i4', count=data_len // 4) def _int_to_real(num): """ @@ -208,7 +209,7 @@ def _pack_int2(data): 6 """ size = len(data) - return struct.pack('>{0}h'.format(size), *data) + return numpy.array(data).astype('>i2').tobytes() def _pack_int4(data): """ @@ -223,7 +224,7 @@ def _pack_int4(data): 12 """ size = len(data) - return struct.pack('>{0}l'.format(size), *data) + return numpy.array(data).astype('>i4').tobytes() def _real_to_int(fnum): """ @@ -344,12 +345,7 @@ class Record(object): if data is not None: self.data = data elif points is not None: - new_data = [] - # TODO make it faster - for point in points: - new_data.append(point[0]) - new_data.append(point[1]) - self.data = new_data + self.data = numpy.ravel(points) elif times is not None: mod_time = times[0] acc_time = times[1] @@ -503,7 +499,7 @@ class Record(object): data_size = len(self.data) if not data_size or (data_size % 2): raise exceptions.DataSizeError(self.tag) - return [(self.data[i], self.data[i+1]) for i in range(0, data_size, 2)] + return numpy.array(self.data).reshape(-1, 2) @property def times(self):