地域以外のものについては、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
を使用して独自のを書くことができませんでした、それは難しいことではありません。インスピレーションのためにthis質問を参照してください。
linuxにすでにある 'cmp'プログラムの何が問題なのですか? http://en.wikipedia.org/wiki/Cmp_(Unix)また、ファイルのサイズが異なる場合(または比較領域のサイズが異なる場合)、結果はどうなりますか? –
「累積差」の定義は、「00 ff」と「ff 00」の累積差が0であることを意味します。 –
より一般的な注記:目的を指定しなければ、他の「差異対策」を求めるのは意味がありません。あなたは2つのファイルを(バイトの)ベクトルとみなし、有限次元のベクトルノルムを使用することができます。 –