C++ ofstream 与 C++ cout 通过管道传送到文件

发布于 2024-08-11 01:08:26 字数 155 浏览 2 评论 0原文

我正在编写一组单元测试,将计算值写入文件。每个测试都会生成一个方阵,其中包含 50,000 到 500,000 个双精度数,我总共有 128 个测试用例组合。

编写 cout 语句然后将输出通过管道传输到文件是否涉及任何重大开销,或者我最好使用 ofstream 直接写入文件吗?

I'm writing a set of unit tests that write calculated values out to files. Each test produces a square matrix that holds anywhere from 50,000 to 500,000 doubles, and I have a total of 128 combinations of test cases.

Is there any significant overhead involved in writing cout statements and then piping that output to files, or would I be better off writing directly to the file using an ofstream?

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

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

发布评论

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

评论(3

凉宸 2024-08-18 01:08:27

这将取决于您的系统和环境。这可能差别很小,但只有一种方法可以确定:尝试两种方法并进行测量。

This is going to be dependent on your system and environment. This likely to be very little difference, but there is only one way to be sure: try both approaches and measure them.

深者入戏 2024-08-18 01:08:27

由于涉及的维度太大,我假设这些文件不适合人类阅读?只需确保将它们写为二进制而不是人类可读的文本,因为这将比使用 ofstream 或管道 cout 之间的差异大得多。

我不知道这是否意味着您必须使用ofstream。我从来没有写过二进制文件,所以我不能说这是否可能......

Since the dimensions involved are so large I'm assuming that these files are not meant to be read by a human being? Just make sure you write them out as binary and not human-readable text because that will make so much more difference than the difference between using ofstream or piping cout.

Whether this means you have to use ofstream or not I don't know. I've never written binary to cout so I can't say whether that's possible...

冰魂雪魄 2024-08-18 01:08:27

正如 Charles Bailey 所说,它取决于实现;接下来的内容主要是针对使用 gnu 工具链的 Linux 实现,但我很难想象它在其他操作系统中会有很大不同。

在 libstdc++ 4.4.2 中:

  • fstream 包含底层 stdio_filebuf,它是 basic_filebuf。这个 basic_filebuf 通过继承 basic_streambuf 包含它自己的缓冲区,并且实际上包含一个 __basic_file,它本身包含一个底层纯 C stdio 抽象(FILE* 或 std::__c_file*),在其中刷新缓冲区。

  • cout 是一个 ostream,使用 stdio_sync_filebuf 进行初始化,stdio_sync_filebuf 本身使用 C 文件抽象 stdout 进行初始化。 stdio_sync_filebuf 调用纯 C stdio 函数。

仅考虑 C++,由于有两层缓冲区,fstream 似乎可能更高效。

仅考虑 C,如果使用在文件中重定向的 stdout 文件描述符分叉进程,则写入新打开的文件(fstream 最后执行的操作)或写入 stdout 之间应该没有区别,因为 fd 无论如何都指向文件(cout 最后做了什么)。

如果我是你,我会使用 fstream,因为这是你的意图。

As Charles Bailey said, it's implementation dependent; what follows is mostly for linux implementation with gnu toolchain, but I hardly imagine it being very different in other os.

In libstdc++ 4.4.2:

  • An fstream contain an underlying stdio_filebuf which is a basic_filebuf. This basic_filebuf contain it's own buffer by inheriting basic_streambuf, and actually contain a __basic_file, itself containing an underlying plain C stdio abstraction (FILE* or std::__c_file*), in which it flush the buffer.

  • cout, which is an ostream is initialized with a stdio_sync_filebuf itself initialized with the C file abstraction stdout. stdio_sync_filebuf call plain C stdio functions.

Considering only C++, it appear that an fstream may be more efficient thanks to two layers of buffer.

Considering C only, if the process is forked with the stdout file descriptor redirected in a file, there should be no difference between writing to a new opened file (what fstream does at the end) or to stdout since the fd point to a file anyway (what cout does at the end).

If I were you, I would use an fstream since it's your intent.

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