In the standard library there is a part for working with input / output: std iostreams . I would like to understand how appropriate it is to use after the appearance of C ++ 11?

Sources for using STD i \ o streams:

  1. Lecture on Yandex-events: Once again on std :: iostreams
  2. Article on habrahabr.ru: How slow are iostreams?

PS:

Anticipating some questions and phrases I want to say that I have already been told that it does not throw exceptions, in this case there is a call to the stream.exceptions(std::ios::failbit | std::ios::badbit | std::ios::eofbit) method stream.exceptions(std::ios::failbit | std::ios::badbit | std::ios::eofbit) . They also said that it is too slow and there is a solution to this too. stream.sync_with_stdio(false) .

  • Alternatively, do you see printf () / scanf ()? - gbg
  • 3
    And what appeared in C ++ 11 that can replace threads? ... - Harry
  • Well, for example, std::ios::failure no longer from std::exception , but from std::system_error . There are other changes. The question is just asked to find out the opinion of more experienced comrades - sys_dev

2 answers 2

Because in C ++ 11, there were no alternative means of I / O, then the question has no meaning. If embedded tools were satisfied before the advent of C ++ 11, then they will continue to satisfy even after its release. If not, then no.

    Concerning the effectiveness - I sketched a simple test, below. The result on my machine in Windows 7 with VC ++ 2015 - when outputting to the console

     cout sync : 3204554 cout async: 3225070 stdout : 1153035 

    when redirecting to a file

     cout sync : 4695 cout async: 4818 stdout : 2334 

    So, of course, stdout is faster, but at least unsafe ... As for sync_with_stdio(false) - it seems like a myth :)

    Source:

     #include <vector> #include <string> #include <iostream> #include <iomanip> #include <cstdio> #include <chrono> using namespace std; int main(int argc, const char * argv[]) { const int Count = 10000; vector<int> data; for(int i = 0; i < Count; ++i) data.push_back(rand()); auto start = chrono::high_resolution_clock::now(); for(auto i: data) { printf("%d ",i); } auto stop = chrono::high_resolution_clock::now(); cout << endl; auto printf_time = chrono::duration_cast<chrono::microseconds>(stop-start).count(); start = chrono::high_resolution_clock::now(); for(auto i: data) { cout << i << " "; } stop = chrono::high_resolution_clock::now(); cout << endl; auto iostream_sync_time = chrono::duration_cast<chrono::microseconds>(stop-start).count(); cout.sync_with_stdio(false); start = chrono::high_resolution_clock::now(); for(auto i: data) { cout << i << " "; } stop = chrono::high_resolution_clock::now(); cout << endl; auto iostream_async_time = chrono::duration_cast<chrono::microseconds>(stop-start).count(); cout << "cout sync : " << iostream_sync_time << endl; cout << "cout async: " << iostream_async_time << endl; cout << "stdout : " << printf_time << endl; } 

    And yet - I just can not understand why the vehicle is linking the issue with C ++ 11. After all, there were no changes in this area.

    • 2
      'sync_with_stdio (false)' looked in the article on habr-e: habrahabr.ru/post/246257 - sys_dev
    • @sys_dev I will gladly believe that this will work for some C ++ implementation and some operating system, but I don’t see much of a difference ... - Harry
    • And why are you not measuring in CPU time? - sys_dev
    • I think it does not matter. By the way, measure and tell the results - especially if you have a different environment, it is interesting to see. Like the cin studies. You can give the text to the finished program, I can look at it myself, and I can write for myself - I feel sorry for the time ... - Harry
    • one
      sync_with_stdio(false) must be called before any output. Those. at the very beginning, so the two sync & async tests do not make sense. But this overall picture will not change, of course. Using a buffer will always give a slower result than direct recording. - ixSci