FILE* 和 ifstream 哪个具有更好的内存使用率?

发布于 2024-08-31 02:31:43 字数 323 浏览 1 评论 0原文

我需要从文件中读取固定数量的字节,其大小约为 50MB。更准确地说,从 YUV 4:2:0 CIF/QCIF 文件中读取帧(每帧约 25KB 到约 100KB)。数字不是很大,但我不希望整个文件都在内存中。我正在使用 C++,在这种情况下,FILE* 或 ifstream 哪个具有更好(更少/最小)的内存使用率?请各位指教。谢谢!

编辑:

我读取固定数量的字节:25KB 或 100KB(取决于 QCIF/CIF 格式)。读数采用二进制模式且仅向前。无需寻找。不需要写作,只需阅读。

编辑:

如果很难识别出更好的文件,那么哪一个不需要将整个文件加载到内存中?

I need to read fixed number of bytes from files, whose sizes are around 50MB. To be more precise, read a frame from YUV 4:2:0 CIF/QCIF files (~25KB to ~100KB per frame). Not very huge number but I don't want whole file to be in the memory. I'm using C++, in such a case, which of FILE* or ifstream has better (less/minimal) memory usage? Please kindly advise. Thanks!

EDIT:

I read fixed number of bytes: 25KB or 100KB (depending on QCIF/CIF format). The reading is in binary mode and forward-only. No seeking needed. No writing needed, only reading.

EDIT:

If identifying better of them is hard, which one does not require loading the whole file into memory?

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

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

发布评论

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

评论(4

要走就滚别墨迹 2024-09-07 02:31:43

不可能说 - 这将取决于实现以及您如何读取数据,而您尚未对此进行描述。一般来说,这里有关性能的问题有些毫无意义,因为它们在很大程度上取决于您对库和语言功能的实际使用、具体实现、您的硬件等。

编辑:要回答您的扩展问题 -这两个库都不需要您将所有内容读入内存。你认为他们为什么会这样做?

Impossible to say - it will depend on the implementation, and how you are reading the data, which you have not described. In general, questions here regarding performance are somewhat pointless, as they depend heavily on your actual usage of library and language features, the specific implementation, your hardware etc. etc.

Edit: To answer your expanded question - neither library requires you read everything into memory. Why would you think they do?

扛起拖把扫天下 2024-09-07 02:31:43

我认为最好的答案是“分析并查看”,但理论上 FILE* 在时间和内存使用方面应该更有效。流确实在原始读/写例程上添加了不同的包装器、错误处理程序等,这可能(在您的特定情况下)影响内存使用。

I think the best answer would be "profile and see", but in theory FILE* should be more efficient in time and memory usage. Streams do add different wrappers, error handlers, etc, etc, etc... over raw reading / writing routines, that could (in your particular case) affect the memory usage.

我喜欢麦丽素 2024-09-07 02:31:43

您可以期望使用 FILE* 获得更小的可执行文件,因为它的支持库比 ifstream 更简单,但其他因素(运行时内存消耗和性能)很少会产生显着差异。但总体而言,较小的收益将针对 FILE*,同样仅仅是因为它更简单。

如果您对文件进行的处理非常基本和/或您不需要解析文本输入文件,则 FILE* 将非常适合您。另一方面,如果相反的情况成立,我会选择 ifstream - 我发现 >>比使用 fscanf 更方便、更安全。

You can expect a smaller executable using FILE*, since its supporting libraries are simpler than ifstream, but the other factors (runtime memory consumption and performace) rarely make a significant difference. But the small gain will be in general towards FILE*, again merely because it's simpler.

If the processing you do with the file is very basic and/or you don't need to parse a text input file, FILE* will suit you well. On the other hand, if the opposite is true, I'd go for ifstream - I find the >> operator a lot handier and safer than using fscanf.

别在捏我脸啦 2024-09-07 02:31:43

就性能而言,您肯定更适合使用 FILE* (我不久前在我的一个项目中对此进行了分析)。内存明智的 iostream 不应该造成大问题,尽管我认为它包装 C 库时会产生一些开销。

Performance wise you're definitely better of with FILE* (I profiled that some time ago in a project of mine). Memory wise iostreams shouldn't pose a big problem, although I think that there is some overhead as it wraps the C library.

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