我应该让 QFile (或 fstream)保持打开状态吗?

发布于 2024-11-10 10:16:06 字数 531 浏览 4 评论 0原文

我通常使用两种方法写入文件,要么使用Qt的QFile,要么使用STL的fstream。

我有一个长时间运行(几分钟)的模拟,它将数据记录到文件中。从性能和设计角度来看,以下做法是个好主意:

  1. 始终保持打开状态
  2. 每次写入时关闭并打开
  3. 介于 (1) 和 (2) 之间的某个位置

这里的几个问题解决了此问题(对于 Perl,对于 fopen),但我没有请参阅 QFile 和 fstream 的任何讨论。前面的答案建议保持打开状态(选项 1)。这仍然是 QFile 和 fstream 的转换吗?

I usually use two methods to write to files, either with Qt's QFile or STL's fstream.

I have a long-running (several minutes) simulation which logs data to a file. Performance-wise and design-wise, is it a good idea to:

  1. Keep it open the whole time
  2. Close and open on every write
  3. Somewhere in-between (1) and (2)

Several question on here address this issue (for Perl, for fopen), but I didn't see any discussion of QFile and fstream. The previous answers suggest to keep it open (option 1). Is this still the cast for QFile and fstream?

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

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

发布评论

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

评论(2

静水深流 2024-11-17 10:16:06

就性能而言,在应用程序的生命周期中保持它打开肯定会更好,因为打开和关闭文件的工作量减少意味着花在做那些不会使应用程序更接近完成的事情上的时间更少,这会减慢程序的速度。至于设计,只需确保在应用程序终止之前关闭文件即可。

QFile 和 fstream 可能在底层使用 fopen、fwrite 等(尽管它当然取决于实现)。所以我敢打赌,任何适用于 FILE* 的内容都将适用于 QFiles 和 fstreams。

Performance wise, it would definitely be better to keep it open for the life of the application, just because less work opening and closing files means less time spent doing things that don't move the application closer to completion, which will slow the program down. As for design, just make sure to close the file before the application terminates.

QFile and fstream probably use fopen, fwrite etc under the hood (although it is of course implementation dependent). So I would bet that anything applying to FILE*s would apply to QFiles and fstreams.

无力看清 2024-11-17 10:16:06

这可能取决于您的 libc 实现,但 fstream 通常使用内存映射文件。这些通常非常高效,并且仅在写入数据页时主内存或交换区。

如果您运行的是 32 位系统,并且这些文件非常大或非常多,那么您可能会遇到耗尽虚拟地址空间的问题(在 Windows 上,大约 2GB 可能会导致此类问题)。看到您只是在记录,这似乎不太可能。

但在这种情况下,简单地关闭文件可能会使情况变得更糟,因为虚拟地址空间可能会变得碎片化。

我建议最好始终保持文件打开,除非您认为会遇到上述问题。如果内存有限,那么刷新数据将减少物理内存需求。

This may be dependent on your libc implementation but fstream is generally uses memory-mapped files. These are generally very efficient, and only main memory or swap when a page of data is written to.

If you are running a 32-bit system and these files are very large or very numerous then you could have issues with exhausting the virtual address space (on windows ~2GB might cause such problems). Seeing as you are simply logging this seems quite unlikely.

But simply closing the files might make it worse in that case because then the virtual address space could become fragmented.

I would advise that it is best to leave the files open at all times unless you think you will run into the issues above. If you are memory constrained then flushing the data will reduce the physical memory requirements.

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