判断两个字符串或二进制文件是否不同的最快方法是什么?

问题描述 投票:0回答:5

我正在编写一个单元测试,需要将结果文件与黄金文件进行比较。最简单的方法是什么?

到目前为止我已经(针对Linux环境):

int result = system("diff file1 file2");

如果

result != 0
,它们是不同的。

c++ string comparison binaryfiles
5个回答
24
投票

如果你想要一个纯C++解决方案,我会做这样的事情

#include <algorithm>
#include <iterator>
#include <string>
#include <fstream>

template<typename InputIterator1, typename InputIterator2>
bool
range_equal(InputIterator1 first1, InputIterator1 last1,
        InputIterator2 first2, InputIterator2 last2)
{
    while(first1 != last1 && first2 != last2)
    {
        if(*first1 != *first2) return false;
        ++first1;
        ++first2;
    }
    return (first1 == last1) && (first2 == last2);
}

bool compare_files(const std::string& filename1, const std::string& filename2)
{
    std::ifstream file1(filename1);
    std::ifstream file2(filename2);

    std::istreambuf_iterator<char> begin1(file1);
    std::istreambuf_iterator<char> begin2(file2);

    std::istreambuf_iterator<char> end;

    return range_equal(begin1, end, begin2, end);
}

它避免将整个文件读入内存,并在文件不同时(或在文件末尾)立即停止。 range_equal 因为

std::equal
不采用一对迭代器来表示第二个范围,并且如果第二个范围较短,则不安全。


7
投票

DaveS的答案开发,首先是检查文件大小

#include <fstream>
#include <algorithm>

bool compare_files(const std::string& filename1, const std::string& filename2)
{
    std::ifstream file1(filename1, std::ifstream::ate | std::ifstream::binary); //open file at the end
    std::ifstream file2(filename2, std::ifstream::ate | std::ifstream::binary); //open file at the end
    const std::ifstream::pos_type fileSize = file1.tellg();

    if (fileSize != file2.tellg()) {
        return false; //different file size
    }

    file1.seekg(0); //rewind
    file2.seekg(0); //rewind

    std::istreambuf_iterator<char> begin1(file1);
    std::istreambuf_iterator<char> begin2(file2);

    return std::equal(begin1,std::istreambuf_iterator<char>(),begin2); //Second argument is end-of-range iterator
}

(我想知道在倒回之前,是否可以使用

fileSize
创建一个更高效的流结束迭代器,通过了解流长度,该迭代器将允许
std::equal
当时处理更多字节)。


2
投票

防止读取这两个文件的一种方法是将黄金文件预先计算为哈希值,例如 md5。然后你只需要检查测试文件。请注意,这可能比仅读取两个文件要慢!

或者,分层检查 - 查看文件大小,如果它们不同,则文件不同,您可以避免冗长的读取和比较操作。


1
投票

这应该有效:

#include <string>
#include <fstream>
#include <streambuf>
#include <iterator>


bool equal_files(const std::string& a, const std::string& b) {
  std::ifstream stream{a};
  std::string file1{std::istreambuf_iterator<char>(stream),
                    std::istreambuf_iterator<char>()};
  
  stream = std::ifstream{b};
  std::string file2{std::istreambuf_iterator<char>(stream),
                    std::istreambuf_iterator<char>()};
  
  return file1 == file2;
}
  

我怀疑这没有

diff
那么快,但它避免了调用
system
。不过,对于测试用例来说应该足够了。


0
投票

可能有点大材小用,但你可以使用 boost/bimap 和 boost/scope_exit 构建一个 SHA-256 哈希表。

这是 Stephan T Lavavej 制作的如何执行此操作的视频(从 8.15 开始): http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Advanced-STL/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-5-of-n

有关算法的更多信息: http://en.wikipedia.org/wiki/SHA-2

© www.soinside.com 2019 - 2024. All rights reserved.