C ++ io streams sync_with_stdio no difference

For some reason, I can't get the output stream to work faster with the line

std::ios_base::sync_with_stdio(false);

included at the beginning of my program. I am testing this with these two programs:

#include <iostream>

int main() {
    for (int i = 0; i < 500000; i++)
        std::cout << "Hello World\n";
}

and

#include <iostream>

int main() {
    std::ios_base::sync_with_stdio(false);
    for (int i = 0; i < 500000; i++)
        std::cout << "Hello World\n";       
}

The runtime for each program is as follows

first_test (synchronized)

real    0m1.095s
user    0m0.472s
sys     0m0.299s

second_test (with sync disabled)

real    0m1.091s
user    0m0.471s
sys     0m0.299s

I am compiling with g ++ -O3 main.cpp. I'm working on Mac 10.11.1.

Is there a way to speed up the execution of the output stream by disabling sync_with_stdio?

+4
source share
1 answer

Is there a way to make the output stream run faster by disabling sync_with_stdio?

, , . I/O /t std::cout/std::cin ( ) / / stdout/stdin ( ):

std::cout << "test";
printf("another test";

, - ( ), , . , , ( , - ).

- , .


, . ? , , . , . , . : , 500000, 1 , .

+3

Source: https://habr.com/ru/post/1618132/


All Articles