如何使用.NET快速比较2个文件? [英] How to compare 2 files fast using .NET?

查看:226
本文介绍了如何使用.NET快速比较2个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

典型方法建议您通过FileStream读取二进制文件,并逐字节比较。

Typical approaches recommend reading the binary via FileStream and comparing it byte-by-byte.


  • 校验和比较例如CRC会更快吗?

  • 可以为文件生成校验和?

推荐答案

校验和比较很可能会更慢

A checksum comparison will be most likely be slower than a byte-by-byte comparison.

为了生成校验和,您需要加载文件的每个字节,并对其执行处理。然后,您必须在第二个文件上执行此操作。处理几乎肯定比比较检查慢。

In order to generate a checksum, you'll need to load each byte of the file, and perform processing on it. You'll then have to do this on the second file. The processing will almost definitely be slower than the comparison check.

对于生成校验和:你可以很容易地使用密码类。以下是使用C#生成MD5校验和的简短示例。

As for generating a checksum: You can do this easily with the cryptography classes. Here's a short example of generating an MD5 checksum with C#.

但是,如果您可以预先计算测试或基本情况的校验和,则校验和可能更快,更有意义。如果你有一个现有文件,并且你正在检查一个新文件是否与现有文件相同,预先计算现有文件上的校验和将意味着只需要做一次DiskIO一次,在新文件。这可能比逐个字节的比较更快。

However, a checksum may be faster and make more sense if you can pre-compute the checksum of the "test" or "base" case. If you have an existing file, and you're checking to see if a new file is the same as the existing one, pre-computing the checksum on your "existing" file would mean only needing to do the DiskIO one time, on the new file. This would likely be faster than a byte-by-byte comparison.

这篇关于如何使用.NET快速比较2个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆