在 C++ 中直接从文件读取数据到 RAM

发布于 2024-07-26 09:08:33 字数 330 浏览 7 评论 0原文

有没有办法直接将二进制文件读入RAM?

我的意思是,有没有办法告诉编译器,这是文件,这是 RAM 块,请将文件内容放入 RAM 中,然后就可以了,尽快。

目前,我正在使用 ifstream 单步执行该文件,将其按 64 位块加载到 RAM(数组)中。 但我认为这一定会减慢它的速度,因为它就像(这里有一个类比)使用顶针将水从杯子(文件)中舀入壶(RAM)中,而不是仅仅拿起杯子然后将所有内容物一次性倒入壶中。

作为一个相对较新的人,我对此可能有完全错误的想法 - 任何指导都会有很大的帮助。

我正在寻找将大文件放入 RAM 的最快方法。

谢谢

Is there a way to directly read a binary file into RAM?

What I mean is, is there a way to tell the compiler, here's the file, here's the block of RAM, please put the file contents in the RAM, off you go, quickly as you can please.

Currently I'm stepping through the file with ifstream loading it into RAM (an array) 64bit block by 64 bit block. But I'm thinking that this must slow it down as it's like (here comes an analogy) using a thimble to bail the water out of a cup (the file) into a jug (the RAM) rather than just picking up the cup and tipping the entire contents into the jug in one go.

As a relative newcomer to this I may have completely the wrong idea about this - any guidence would be a great help.

I'm just after the quickest way to get a big file into RAM.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

娇女薄笑 2024-08-02 09:08:33

是什么阻止您一次性读取该文件? 是不是内存太大了?

还可以使用映射文件,UNIX:mmap,Windows:CreateFileMapping

What prevents you from reading the file in one pass? Is it too big to fit in memory?

You can also use mapped file, UNIX : mmap, Windows : CreateFileMapping

池予 2024-08-02 09:08:33

我认为您需要的是 内存映射文件,但是 API 取决于您使用的操作系统是的,在 UNIX 中我相信它是 mmap()。

I think what you need is memory mapped file, however the API depends on which OS you are on, in UNIX I believe it's mmap().

禾厶谷欠 2024-08-02 09:08:33

mmap 应该适合你。 大多数实现在您引用实际内存页之前不会真正读取文件,但在任何情况下您都可以访问文件内容,就像它始终位于 RAM 中一样。

mmap should work for you. Most implementations do not actually read the file until you reference the actual memory pages, but in any case you can access the file contents as if it was in RAM all the time.

乱世争霸 2024-08-02 09:08:33

您没有说明您所在的平台,但这听起来像是内存映射文件的工作。

You don't say which platform you are on, but this sounds like a job for memory-mapped files.

怪我鬧 2024-08-02 09:08:33

你的程序被赋予了虚拟内存——其中一些映射到RAM,一些映射到交换文件,你对此无能为力。 但是,您可以通过复制更大的块(例如几兆字节)来减少使用文件系统接口的开销。

Your program is given virtual memory - some of it is mapped onto RAM, some onto swap file and you can't do much about it. However you can reduce the overhead of using the filesystem interface by just copying bigger blocks - several megabytes for example.

辞别 2024-08-02 09:08:33

我不确定这是否是您所要求的,但为了加快从文件中的读取速度,您可以缓冲更多字节。 fstream 的 read() 方法的第二个参数可以设置为指定输入大小。 例如,给他一个 32k (32*(2^10)) 值可以加快阅读过程。 我建议您使用不同的缓冲区大小进行一些基准测试。

如果你的意思是“避免将文件放在交换区并始终放在RAM中”,我想你可以,因为这是操作系统负责的事情。

I'm not sure if this is what you're asking, but to speed up reading from a file you could buffer more bytes. the second argument of the read() method of fstream can be set to specify the input size. giving him, for example, a 32k (32*(2^10)) value could speed up the reading process. I suggest you do some benchmarking with different buffer size.

If you mean "avoiding to put the file on swap and put it ALWAYS in RAM" I think you can, since this is something the OS takes care.

久伴你 2024-08-02 09:08:33

查看 DirectX SDK 中的 ContentStreaming 示例。

它使用文件映射方法。 显然,它使用的是Windows API,但是您也可以找到其他平台的相应API。

有趣的是,它向您展示了上下文中的方法。 (按需读入/分页大型模型的某些部分(在本例中为地形)

Look at the ContentStreaming example in the DirectX SDK.

It uses the file mapping approach. Obviously, its uses the windows API, but you can find the corresponding API for other platforms as well.

The interesting thing is that it shows you the approach in context. (Reading in/paging in parts of a huge model (in this case terrain) on demand

月竹挽风 2024-08-02 09:08:33

Boost 为内存映射文件提供跨平台支持,等等...

增强内存映射文件

Boost provides cross platform support for memory mapped files, among other things...

Boost Memory Mapped files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文