对于除区域之外的所有内容,您都可以使用numpy。像这样的东西(未经测试):
import numpy as np
a = np.fromfile("file A", dtype="uint8")
b = np.fromfile("file B", dtype="uint8")
# Compute the number of bytes that are different
different_bytes = np.sum(a != b)
# Compute the sum of the differences
difference = np.sum(a - b)
# Compute the sum of the absolute value of the differences
absolute_difference = np.sum(np.abs(a - b))
# In some cases, the number of bits that have changed is a better
# measurement of change. To compute it we make a lookup array where
# bitcount_lookup[byte] == number_of_1_bits_in_byte (so
# bitcount_lookup[0:16] == [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4])
bitcount_lookup = np.array(
[bin(i).count("1") for i in range(256)], dtype="uint8")
# Numpy allows using an array as an index. ^ computes the XOR of
# each pair of bytes. The result is a byte with a 1 bit where the
# bits of the input differed, and a 0 bit otherwise.
bit_diff_count = np.sum(bitcount_lookup[a ^ b])
我找不到用于计算区域的 numpy 函数,但只需编写自己的a != b
用作输入,它应该不难。请参阅此问题以获取灵感。