@Bakuriu可能是正确的,这可能是微观优化.你的瓶颈几乎肯定是IO,之后就是减压.分配内存两次可能并不重要.
但是,如果您想避免额外的内存分配,可以使用numpy.frombuffer将字符串视为numpy数组.
这避免了重复内存(字符串和数组使用相同的内存缓冲区),但默认情况下该数组将是只读的.如果需要,您可以将其更改为允许写入.
在你的情况下,它就像用frombuffer替换fromstring一样简单:
f = gzip.GzipFile(filename)
f.read(10000) # fixed length ascii header
event_dtype = np.dtype([
('Id', '>u4'), # simplified
('UnixTimeUTC', '>u4', 2),
('Data', '>i2', (1600,1024) )
])
s = f.read( event_dtype.itemsize )
event = np.frombuffer(s, dtype=event_dtype, count=1)
只是为了证明使用这种方法不重复内存:
import numpy as np
x = "hello"
y = np.frombuffer(x, dtype=np.uint8)
# Make "y" writeable...
y.flags.writeable = True
# Prove that we're using the same memory
y[0] = 121
print x #
这产生:黄色而不是你好.
无论在这种特殊情况下它是否是一个重要的优化,它都是一个有用的方法需要注意.