最快读取二进制文件的读写 [英] Fastest reading binary file reading and writing

查看:480
本文介绍了最快读取二进制文件的读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序来读取和解析大小可能在1 KB到200 MB之间的文件.

I am writing an application to read and parse files which may be 1 KB to 200 MB in size.

我必须解析两次...

I have to parse it two times...

  1. 提取文件中包含的图像.

  1. Extract an image contained in the file.

解析该图像以提取图像的内容.

Parse that image To extract the contents of the image.

我通常使用文件流,缓冲流,二进制读取器和二进制写入器来读取和写入内容.

I generally use the file stream, buffered stream, binary reader and binary writer to read and write the contents.

现在,我想知道最快,最有效的方式来读取文件并提取内容...

Now, I want to know the fastest and most efficient way to read the file and extract the contents...

有没有好的方法或好的类库?

Is there a good method or a good class library?

注意:不安全的代码是可以的!

NOTE: Unsafe code is OK!

推荐答案

读取文件最快,最简单的方法就是:

The fastest and simplest way to read the file is simply:

var file = File.ReadAllBytes(fileName);

这会将整个文件作为字节数组读取到内存中.然后,您可以遍历它,以所需的内存阵列访问速度(即非常快)来查找所需内容.几乎可以肯定,这比尝试在读取文件时处理文件要快.

That will read the entire file as a byte array into memory. You can then go through it looking for what you need at memory array access speed (which is to say, extremely fast). This will almost certainly be faster than trying to process the file as you read it.

但是,如果此文件不能舒适地容纳在内存中(并且81 MB可以容纳),那么您将需要分批执行此操作.如果不需要这样做,我们可以安全地避免进行棘手的讨论.在这种情况下,解决方案可以是:

However, if this file will not comfortably fit in memory (and 81 MB will), then you will need to do this in chunks. If this is not needed, we can safely avoid that tricky discussion. The solutions in this case will be either:

  1. 如果使用.NET 4.0,请使用内存映射文件( 中的更多内容)文件? ).

如果没有,您将需要分块读取,缓存并保留您认为在内存中需要的内容(以提高效率),或者重新读取,您根本无法将其保留在内存中.这会变得混乱而缓慢.

If not, you'll need to chunk read, cache and keep around what you think you'll need in memory (for efficiency) or re-reading it you simply can't keep it in memory. This can become messy and slow.

这篇关于最快读取二进制文件的读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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