python - 使用 struct.unpack VS np.frombuffer VS np.ndarray VS np.fromfile 解压二进制文件

标签 python numpy struct

我正在解压缩具有许多不同数据类型的大型二进制文件 (~1GB)。我正处于创建循环以隐藏每个字节的早期阶段。 我一直在使用 struct.unpack,但最近认为如果我使用 numpy 它会运行得更快。然而,切换到 numpy 已经减慢了我的程序。 我试过:

struct.unpack
np.fromfile
np.frombuffer
np.ndarray

注意:在 np.fromfile 方法中,我将文件保持打开状态,不将其加载到内存中并进行搜索

1)

with open(file="file_loc" , mode='rb') as file: 
    RAW = file.read()
byte=0
len = len(RAW)
while( byte < len):
    header = struct.unpack(">HHIH", RAW[byte:(byte+10)])
    size = header[1]
    loc  = str(header[3])
    data[loc] = struct.unpack(">B", RAW[byte+10:byte+size-10)
    byte+=size

2)

dt=('>u2,>u2,>u4,>u2')
with open(file="file_loc" , mode='rb') as RAW:
    same loop as above:
        header = np.fromfile(RAW[byte:byte+10], dtype=dt, count=1)[0]
        data   = np.fromfile(RAW[byte+10:byte+size-10], dtype=">u1", count=size-10)

3)

dt=('>u2,>u2,>u4,>u2')
with open(file="file_loc" , mode='rb') as file:
    RAW = file.read()
same loop:
    header = np.ndarray(buffer=RAW[byte:byte+10], dtype=dt_header, shape= 1)[0]
    data   = np.ndarray(buffer=RAW[byte+10:byte+size-10], dtype=">u1", shape=size-10)

4) 除了使用 np.frombuffer() 与 3 几乎相同

所有 numpy 实现的处理速度大约是 struct.unpack 方法的一半,这不是我所期望的。

如果我可以做些什么来提高性能,请告诉我。

另外,我只是凭内存输入这个,所以它可能有一些错误。

最佳答案

我没有太多地使用 struct,但是在您的代码和文档之间,我让它在存储整数数组的缓冲区上工作。

numpy 数组创建一个字节数组/字符串。

In [81]: arr = np.arange(1000)
In [82]: barr = arr.tobytes()
In [83]: type(barr)
Out[83]: bytes
In [84]: len(barr)
Out[84]: 8000

反过来就是tobytes:

In [85]: x = np.frombuffer(barr, dtype=int)
In [86]: x[:10]
Out[86]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [87]: np.allclose(x,arr)
Out[87]: True

ndarray 也可以,但通常不鼓励直接使用此构造函数:

In [88]: x = np.ndarray(buffer=barr, dtype=int, shape=(1000,))
In [89]: np.allclose(x,arr)
Out[89]: True

要使用struct,我需要创建一个包含长度“1000 long”的格式:

In [90]: tup = struct.unpack('1000l', barr)
In [91]: len(tup)
Out[91]: 1000
In [92]: tup[:10]
Out[92]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
In [93]: np.allclose(np.array(tup),arr)
Out[93]: True

现在我们已经建立了读取缓冲区的等效方法,做一些计时:

In [94]: timeit x = np.frombuffer(barr, dtype=int)
617 ns ± 0.806 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [95]: timeit x = np.ndarray(buffer=barr, dtype=int, shape=(1000,))
1.11 µs ± 1.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [96]: timeit tup = struct.unpack('1000l', barr)
19 µs ± 38.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [97]: timeit tup = np.array(struct.unpack('1000l', barr))
87.5 µs ± 25.1 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

frombuffer 看起来不错。

你的 struct.unpack 循环让我很困惑。我不认为它与 frombuffer 做同样的事情。但是就像一开始所说的那样,我没有太多地使用 struct

关于python - 使用 struct.unpack VS np.frombuffer VS np.ndarray VS np.fromfile 解压二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54679949/

相关文章:

python - 使用另一个二维数组的索引提取二维数组的元素

python - 无法使用 requests.post 登录网站 - 指定 URL

python - numpy 中结构化数组的 ndim?

c - 结构中的函数指针,但在 C 中具有不同原型(prototype)的函数

c - 使用 realloc 分配内存,我需要的确切大小

android - Python 和 openCV 到 android APK

python - 在以交互模式运行脚本之前,如何将脚本传递给 Sage?

python - numpy 数组的时髦行为

python - 使用 pyc 编译 IronPython - 没有名为 numpy 的模块

c++ - 初始化结构包含对结构的引用