zhangsongcui

Test the speed of iostream

Nov 11th, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. // g++ -O3 std=c++0x filename.cpp -o filename.out
  2. #include <chrono>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstdlib>
  6.  
  7. char buf[4096];
  8.  
  9. void clrscr()
  10. {
  11. #ifdef _WIN32
  12.     system("CLS");
  13. #else
  14.     system("clear");
  15. #endif
  16. }
  17.  
  18. int main()
  19. {
  20.     using namespace std;
  21.     enum { TIMES = 40960 };
  22. #ifdef USE_BUFFER
  23.     cout.rdbuf()->pubsetbuf(buf, sizeof(buf));
  24. #endif
  25.     getchar();
  26.  
  27.     clrscr();
  28.     auto timer = std::chrono::system_clock::now();
  29.     for (int i=0; i!=TIMES; ++i)
  30.         cout << "asdfghjklasdfghjklasdfghjkl;asdfghjklasdfghjklasdfghjkl;\n";
  31.     cout.flush();
  32.     auto t1 = std::chrono::system_clock::now() - timer;
  33.  
  34.     clrscr();
  35.     timer = std::chrono::system_clock::now();
  36.     for (int i=0; i!=TIMES; ++i)
  37.         printf("%s", "asdfghjklasdfghjklasdfghjkl;asdfghjklasdfghjklasdfghjkl;\n");
  38.     fflush(stdout);
  39.     auto t2 = std::chrono::system_clock::now() - timer;
  40.  
  41.     clrscr();
  42.     timer = std::chrono::system_clock::now();
  43.     for (int i=0; i!=TIMES; ++i)
  44.         printf("asdfghjklasdfghjklasdfghjkl;asdfghjklasdfghjklasdfghjkl;\n");
  45.     fflush(stdout);
  46.     auto t3 = std::chrono::system_clock::now() - timer;
  47.  
  48.     clrscr();
  49.     cout << t1.count() << endl << t2.count() << endl << t3.count() << endl;
  50. }
Add Comment
Please, Sign In to add comment