How can I measure the difference bit per bit or byte per byte of two files that should be identical but are not?
In the end I'd like to have a ratio of similarity, like in a percentual score.
OS: Ubuntu
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This tool is specifically designed for binary diffs:
bash
# Install xdelta3
sudo apt install xdelta3
# Create a delta file
xdelta3 -e -s file1 file2 delta
# The delta file size indicates how different the files are
ls -l delta
Python script for exact similarity percentage
For precise control, here's a Python script:
python
#!/usr/bin/env python3
import sys
def compare_files(file1, file2):
with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
data1 = f1.read()
data2 = f2.read()
# Handle different file sizes
max_len = max(len(data1), len(data2))
min_len = min(len(data1), len(data2))
# Count matching bytes
matches = sum(1 for i in range(min_len) if data1[i] == data2[i])
# Account for size difference
total_diff = (max_len - min_len) + (min_len - matches)
similarity = (max_len - total_diff) / max_len * 100
print(f"File sizes: {len(data1)} vs {len(data2)} bytes")
print(f"Matching bytes: {matches}")
print(f"Similarity: {similarity:.2f}%")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: compare.py file1 file2")
sys.exit(1)
compare_files(sys.argv[1], sys.argv[2])
Save this as compare.py, make it executable (chmod +x compare.py), and run:
The Python script gives you the most flexibility and accurate percentage, while cmp with some shell arithmetic is the quickest for simple cases. Choose based on your specific needs and whether you want to see where the differences occur or just get a similarity score.