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

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

问题描述

推荐阅读通过的FileStream二进制和字节字节进行比较典型的做法。

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


  • 会校验比较如CRC更快?

  • 是否有可以生成一个文件的校验和任何.NET库?

推荐答案

一个校验和比较,将最有可能会比逐字节的比较慢。

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#.

然而,校验可能会更快和更有意义,如果你能pre-计算测试或基地案件的校验和。如果您有现成的文件,并且你检查,看看是否有新的文件是一样的现有的,pre计算你的现有文件的校验将意味着只需要做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快速比较两个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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