混合 cout 和 printf 以加快输出速度

发布于 2024-08-15 05:05:00 字数 620 浏览 13 评论 0原文

执行一些测试后,我注意到 printfcout 快得多。我知道它取决于实现,但在我的 Linux 机器上 printf 速度快了 8 倍。所以我的想法是混合两种打印方法:我想使用 cout 进行简单打印,并计划使用 printf 生成大量输出(通常在循环中) 。我认为只要我在切换到其他方法之前不忘记冲洗就可以安全地这样做:

cout << "Hello" << endl;
cout.flush();

for (int i=0; i<1000000; ++i) {
    printf("World!\n");
}
fflush(stdout);

cout << "last line" << endl;
cout << flush;

这样可以吗?

更新:感谢您的所有宝贵反馈。答案摘要:如果您想避免棘手的解决方案,只需坚持使用 cout 但不要使用 endl 因为它会隐式刷新缓冲区(减慢进程)。请改用 "\n"。如果您产生大量输出,这可能会很有趣。

After performing some tests I noticed that printf is much faster than cout. I know that it's implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and I plan to use printf for producing huge outputs (typically in a loop). I think it's safe to do as long as I don't forget to flush before switching to the other method:

cout << "Hello" << endl;
cout.flush();

for (int i=0; i<1000000; ++i) {
    printf("World!\n");
}
fflush(stdout);

cout << "last line" << endl;
cout << flush;

Is it OK like that?

Update: Thanks for all the precious feedbacks. Summary of the answers: if you want to avoid tricky solutions, simply stick with cout but don't use endl since it flushes the buffer implicitly (slowing the process down). Use "\n" instead. It can be interesting if you produce large outputs.

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

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

发布评论

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

评论(9

山田美奈子 2024-08-22 05:05:00

直接的答案是,是的,没关系。

许多人对如何提高速度提出了各种想法,但对于哪种方法最有效似乎存在很大分歧。我决定编写一个快速测试程序,以至少了解哪些技术做了什么。

#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <stdio.h>

char fmt[] = "%s\n";
static const int count = 3000000;
static char const *const string = "This is a string.";
static std::string s = std::string(string) + "\n";

void show_time(void (*f)(), char const *caption) { 
    clock_t start = clock();
    f();
    clock_t ticks = clock()-start;
    std::cerr << std::setw(30) << caption 
        << ": " 
        << (double)ticks/CLOCKS_PER_SEC << "\n";
}

void use_printf() {
    for (int i=0; i<count; i++)
        printf(fmt, string);
}

void use_puts() {
    for (int i=0; i<count; i++) 
        puts(string);        
}

void use_cout() { 
    for (int i=0; i<count; i++)
        std::cout << string << "\n";
}

void use_cout_unsync() { 
    std::cout.sync_with_stdio(false);
    for (int i=0; i<count; i++)
        std::cout << string << "\n";
    std::cout.sync_with_stdio(true);
}

void use_stringstream() { 
    std::stringstream temp;
    for (int i=0; i<count; i++)
        temp << string << "\n";
    std::cout << temp.str();
}

void use_endl() { 
    for (int i=0; i<count; i++)
        std::cout << string << std::endl;
}

void use_fill_n() { 
    std::fill_n(std::ostream_iterator<char const *>(std::cout, "\n"), count, string);
}

void use_write() {
    for (int i = 0; i < count; i++)
        std::cout.write(s.data(), s.size());
}

int main() { 
    show_time(use_printf, "Time using printf");
    show_time(use_puts, "Time using puts");
    show_time(use_cout, "Time using cout (synced)");
    show_time(use_cout_unsync, "Time using cout (un-synced)");
    show_time(use_stringstream, "Time using stringstream");
    show_time(use_endl, "Time using endl");
    show_time(use_fill_n, "Time using fill_n");
    show_time(use_write, "Time using write");
    return 0;
}

我用 VC++ 2013(x86 和 x64 版本)编译后在 Windows 上运行了这个。一次运行的输出(输出重定向到磁盘文件)如下所示:

          Time using printf: 0.953
            Time using puts: 0.567
   Time using cout (synced): 0.736
Time using cout (un-synced): 0.714
    Time using stringstream: 0.725
            Time using endl: 20.097
          Time using fill_n: 0.749
           Time using write: 0.499

正如预期的那样,结果有所不同,但有几点我发现很有趣:

  1. printf/puts 在写入 NUL 设备时比 cout 快得多,
    • 但 cout 保持不变写入真实文件时效果非常好
  2. 相当多的建议优化收效甚微
    • 在我的测试中,fill_n 与其他任何东西一样快
  3. 到目前为止,最大的优化是避免 endl
  4. cout.write 给出最快的时间(尽管可能不是很大,

我最近编辑了代码以强制调用 printf。Anders Kaseorg 很友善地指出 - g++ > 识别特定序列 printf("%s\n", foo); 相当于 puts(foo);,并相应地生成代码(即生成代码调用 puts 而不是 printf)。将格式字符串移动到全局数组,并将其作为格式字符串传递,会产生相同的输出,但强制通过 生成它。 >printf 而不是 puts 当然,他们也可能有一天会对此进行优化,但至少现在(g++ 5.1)使用 g++ -O3 进行测试。 -S 确认它实际上正在调用 printf(其中先前的代码编译为对 puts 的调用)。

The direct answer is that yes, that's okay.

A lot of people have thrown around various ideas of how to improve speed, but there seems to be quite a bit of disagreement over which is most effective. I decided to write a quick test program to get at least some idea of which techniques did what.

#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <stdio.h>

char fmt[] = "%s\n";
static const int count = 3000000;
static char const *const string = "This is a string.";
static std::string s = std::string(string) + "\n";

void show_time(void (*f)(), char const *caption) { 
    clock_t start = clock();
    f();
    clock_t ticks = clock()-start;
    std::cerr << std::setw(30) << caption 
        << ": " 
        << (double)ticks/CLOCKS_PER_SEC << "\n";
}

void use_printf() {
    for (int i=0; i<count; i++)
        printf(fmt, string);
}

void use_puts() {
    for (int i=0; i<count; i++) 
        puts(string);        
}

void use_cout() { 
    for (int i=0; i<count; i++)
        std::cout << string << "\n";
}

void use_cout_unsync() { 
    std::cout.sync_with_stdio(false);
    for (int i=0; i<count; i++)
        std::cout << string << "\n";
    std::cout.sync_with_stdio(true);
}

void use_stringstream() { 
    std::stringstream temp;
    for (int i=0; i<count; i++)
        temp << string << "\n";
    std::cout << temp.str();
}

void use_endl() { 
    for (int i=0; i<count; i++)
        std::cout << string << std::endl;
}

void use_fill_n() { 
    std::fill_n(std::ostream_iterator<char const *>(std::cout, "\n"), count, string);
}

void use_write() {
    for (int i = 0; i < count; i++)
        std::cout.write(s.data(), s.size());
}

int main() { 
    show_time(use_printf, "Time using printf");
    show_time(use_puts, "Time using puts");
    show_time(use_cout, "Time using cout (synced)");
    show_time(use_cout_unsync, "Time using cout (un-synced)");
    show_time(use_stringstream, "Time using stringstream");
    show_time(use_endl, "Time using endl");
    show_time(use_fill_n, "Time using fill_n");
    show_time(use_write, "Time using write");
    return 0;
}

I ran this on Windows after compiling with VC++ 2013 (both x86 and x64 versions). Output from one run (with output redirected to a disk file) looked like this:

          Time using printf: 0.953
            Time using puts: 0.567
   Time using cout (synced): 0.736
Time using cout (un-synced): 0.714
    Time using stringstream: 0.725
            Time using endl: 20.097
          Time using fill_n: 0.749
           Time using write: 0.499

As expected, results vary, but there are a few points I found interesting:

  1. printf/puts are much faster than cout when writing to the NUL device
    • but cout keeps up quite nicely when writing to a real file
  2. Quite a few proposed optimizations accomplish little
    • In my testing, fill_n is about as fast as anything else
  3. By far the biggest optimization is avoiding endl
  4. cout.write gave the fastest time (though probably not by a significant margin

I've recently edited the code to force a call to printf. Anders Kaseorg was kind enough to point out--that g++ recognizes the specific sequence printf("%s\n", foo); is equivalent to puts(foo);, and generates code accordingly (i.e., generates code to call puts instead of printf). Moving the format string to a global array, and passing that as the format string produces identical output, but forces it to be produced via printf instead of puts. Of course, it's possible they might optimize around this some day as well, but at least for now (g++ 5.1) a test with g++ -O3 -S confirms that it's actually calling printf (where the previous code compiled to a call to puts).

ら栖息 2024-08-22 05:05:00

向流发送 std::endl 会附加一个换行符 并刷新流。随后调用 cout.flush() 是多余的。如果在计时 coutprintf 时这样做,那么您就不是在比较苹果与苹果。

Sending std::endl to the stream appends a newline and flushes the stream. The subsequent invocation of cout.flush() is superfluous. If this was done when timing cout vs. printf then you were not comparing apples to apples.

小糖芽 2024-08-22 05:05:00

默认情况下,C 和 C++ 标准输出流是同步的,因此写入其中一个会导致另一个的刷新,因此不需要显式刷新。

By default, the C and C++ standard output streams are synchronised, so that writing to one causes a flush of the other, so explicit flushes are not needed.

冰火雁神 2024-08-22 05:05:00

另请注意,C++ 流已同步到 C 流。
因此,它需要做额外的工作来保持同步。

另一件需要注意的事情是确保冲洗流的量相等。如果您连续刷新一个系统上的流而不是另一个系统上的流,这肯定会影响测试的速度。

在假设其中一个比另一个更快之前,您应该:

  • 从 CI/O 取消同步 C++ I/O(请参阅sync_with_stdio())。
  • 确保冲水量具有可比性。

Also, note that the C++ stream is synced to the C stream.
Thus it does extra work to stay in sync.

Another thing to note is to make sure you flush the streams an equal amount. If you continuously flush the stream on one system and not the other that will definitely affect the speed of the tests.

Before assuming that one is faster than the other you should:

  • un-sync C++ I/O from C I/O (see sync_with_stdio() ).
  • Make sure the amount of flushes is comparable.
一身仙ぐ女味 2024-08-22 05:05:00

您可以通过增加 stdout 的缓冲区大小来进一步提高 printf 的性能:

setvbuf (stdout, NULL, _IOFBF, 32768);  // any value larger than 512 and also a
                  // a multiple of the system i/o buffer size is an improvement

对操作系统执行 I/O 的调用次数几乎总是最昂贵的组件和性能限制器。

当然,如果 cout 输出与 stdout 混合,则缓冲区刷新会达不到增加缓冲区大小的目的。

You can further improve the performance of printf by increasing the buffer size for stdout:

setvbuf (stdout, NULL, _IOFBF, 32768);  // any value larger than 512 and also a
                  // a multiple of the system i/o buffer size is an improvement

The number of calls to the operating system to perform i/o is almost always the most expensive component and performance limiter.

Of course, if cout output is intermixed with stdout, the buffer flushes defeat the purpose an increased buffer size.

无风消散 2024-08-22 05:05:00

您可以使用 sync_with_stdio 使 C++ IO 更快。

cout.sync_with_stdio(false);

应该使用 cout 提高输出性能。

You can use sync_with_stdio to make C++ IO faster.

cout.sync_with_stdio(false);

Should improve your output perfomance with cout.

临走之时 2024-08-22 05:05:00

不必担心 printfcout 之间的性能。如果您想提高性能,请将格式化输出与非格式化输出分开。

puts("Hello World\n")printf("%s", "Hellow World\n") 快得多。 (主要是由于格式化开销)。一旦将格式化与纯文本分离,您可以执行以下技巧:

const char hello[] = "Hello World\n";
cout.write(hello, sizeof(hello) - sizeof('\0'));

要加速格式化输出,技巧是将所有格式化执行为字符串,然后将块输出与字符串(或缓冲区)结合使用:

const unsigned int MAX_BUFFER_SIZE = 256;
char buffer[MAX_BUFFER_SIZE];
sprintf(buffer, "%d times is a charm.\n", 5);
unsigned int text_length = strlen(buffer) - sizeof('\0');
fwrite(buffer, 1, text_length, stdout);

要进一步提高程序的性能,请减少输出数量。你输出的东西越少,你的程序就会越快。副作用是您的可执行文件大小也会缩小。

Don't worry about the performance between printf and cout. If you want to gain performance, separate formatted output from non-formatted output.

puts("Hello World\n") is much faster than printf("%s", "Hellow World\n"). (Primarily due to the formatting overhead). Once you have isolated the formatted from plain text, you can do tricks like:

const char hello[] = "Hello World\n";
cout.write(hello, sizeof(hello) - sizeof('\0'));

To speed up formatted output, the trick is to perform all formatting to a string, then use block output with the string (or buffer):

const unsigned int MAX_BUFFER_SIZE = 256;
char buffer[MAX_BUFFER_SIZE];
sprintf(buffer, "%d times is a charm.\n", 5);
unsigned int text_length = strlen(buffer) - sizeof('\0');
fwrite(buffer, 1, text_length, stdout);

To further improve your program's performance, reduce the quantity of output. The less stuff you output, the faster your program will be. A side effect will be that your executable size will shrink too.

想念有你 2024-08-22 05:05:00

好吧,老实说,我想不出任何实际使用 cout 的理由。使用一个庞大的模板来做一些如此简单的事情,并且将在每个文件中出现,这是完全疯狂的。而且,它的设计就好像它的打字速度尽可能慢,并且在输入一百万次之后<<<<然后在中间输入值并得到类似 >variableName>>> 的内容意外的话我再也不想这样做了。

更不用说如果你包含 std 命名空间,世界最终会崩溃,如果你不包含,你的打字负担就会变得更加荒谬。

不过我也不太喜欢 printf 。对我来说,解决方案是创建我自己的具体类,然后调用其中所需的任何 io 内容。然后你可以以任何你想要的方式、任何你想要的实现、任何你想要的格式等来拥有非常简单的 io (通常你希望浮点数始终是一种方式,例如,不要无缘无故地格式化它们 800 种方式,所以把每次通话的格式化都是一个笑话)。

所以我输入的都是类似的东西
doout+“这比“+debugIoType+”的“+cPlusPlusMethod+”更理智。至少在我看来”;
杜特++;

但你可以拥有任何你想要的。对于大量文件,令人惊讶的是这也大大缩短了编译时间。

另外,混合 C 和 C++ 并没有什么问题,应该明智地进行,如果您使用的东西首先导致使用 C 时出现问题,那么可以肯定地说,您最不用担心的是混合 C 和 C 带来的麻烦。 C++。

Well, I can't think of any reason to actually use cout to be honest. It's completely insane to have a huge bulky template to do something so simple that will be in every file. Also, it's like it's designed to be as slow to type as possible and after the millionth time of typing <<<< and then typing the value in between and getting something lik >variableName>>> on accident I never want to do that again.

Not to mention if you include std namespace the world will eventually implode, and if you don't your typing burden becomes even more ridiculous.

However I don't like printf a lot either. For me, the solution is to create my own concrete class and then call whatever io stuff is necessary within that. Then you can have really simple io in any manner you want and with whatever implementation you want, whatever formatting you want, etc (generally you want floats to always be one way for example, not to format them 800 ways for no reason, so putting in formatting with every call is a joke).

So all I type is something like
dout+"This is more sane than "+cPlusPlusMethod+" of "+debugIoType+". IMO at least";
dout++;

but you can have whatever you want. With lots of files it's surprising how much this improves compile time, too.

Also, there's nothing wrong with mixing C and C++, it should just be done jusdiciously and if you are using the things that cause the problems with using C in the first place it's safe to say the least of your worries is trouble from mixing C and C++.

孤芳又自赏 2024-08-22 05:05:00

我的 C++ 书籍(仅供参考)建议不要混合使用 C++ 和 C iomethods。我很确定 C 函数践踏了 C++ 期望/保持的状态。

Mixing C++ and C iomethods was recommended against by my C++ books, FYI. I'm pretty sure the C functions trample on the state expected/held by C++.

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