fork download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. class Duration {
  7. std::chrono::high_resolution_clock::time_point beg, duration;
  8. public:
  9. Duration() {
  10. beg = std::chrono::high_resolution_clock::now();
  11. }
  12. ~Duration() {
  13. duration = std::chrono::high_resolution_clock::now();
  14. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration - beg).count() << "ms" << std::endl;
  15. }
  16. };
  17.  
  18. const int N = 10000000;
  19.  
  20. int rnd[N];
  21.  
  22. void func1()
  23. {
  24. return;
  25. }
  26.  
  27. void func2()
  28. {
  29. return;
  30. }
  31.  
  32. void func3()
  33. {
  34. return;
  35. }
  36.  
  37. void func4()
  38. {
  39. return;
  40. }
  41.  
  42. int main()
  43. {
  44. void (*func[])() = { func1, func2, func3, func4 }; // Avoid std::function costs
  45.  
  46. {
  47. std::cout << "The same function call" << std::endl;
  48. Duration d;
  49.  
  50. for (int i = 0; i < N; i++)
  51. func[0]();
  52. }
  53.  
  54. {
  55. std::srand(std::time(0));
  56. for (int i = 0; i < N; i++)
  57. rnd[i] = std::rand() % 4; // Avoid std::rand() costs
  58. std::cout << "Random function call" << std::endl;
  59. Duration d;
  60.  
  61. for (int i = 0; i < N; i++)
  62. func[rnd[i]]();
  63. }
  64. }
  65.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.8/chrono:35:0,
                 from prog.cpp:2:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
prog.cpp:7:3: error: ‘chrono’ in namespace ‘std’ does not name a type
   std::chrono::high_resolution_clock::time_point beg, duration;
   ^
prog.cpp: In constructor ‘Duration::Duration()’:
prog.cpp:10:5: error: ‘beg’ was not declared in this scope
     beg = std::chrono::high_resolution_clock::now();
     ^
prog.cpp:10:16: error: ‘std::chrono’ has not been declared
     beg = std::chrono::high_resolution_clock::now();
                ^
prog.cpp: In destructor ‘Duration::~Duration()’:
prog.cpp:13:5: error: ‘duration’ was not declared in this scope
     duration = std::chrono::high_resolution_clock::now();
     ^
prog.cpp:13:21: error: ‘std::chrono’ has not been declared
     duration = std::chrono::high_resolution_clock::now();
                     ^
prog.cpp:14:23: error: ‘std::chrono’ has not been declared
     std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration - beg).count() << "ms" << std::endl;
                       ^
prog.cpp:14:50: error: ‘std::chrono’ has not been declared
     std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration - beg).count() << "ms" << std::endl;
                                                  ^
prog.cpp:14:83: error: ‘beg’ was not declared in this scope
     std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration - beg).count() << "ms" << std::endl;
                                                                                   ^
stdout
Standard output is empty