fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /// the following three lines are for debugging purposes only
  6. #ifdef ONLINE_JUDGE
  7. #undef ONLINE_JUDGE
  8. #endif
  9.  
  10. /// The time_mointor() code starts here
  11. inline void time_monitor( const string& s, const function< void() >& procedure )
  12. {
  13. #ifndef ONLINE_JUDGE
  14. typedef chrono::high_resolution_clock clock_t;
  15.  
  16. auto start_time = clock_t::now();
  17. #endif // ONLINE_JUDGE
  18.  
  19. procedure();
  20.  
  21. #ifndef ONLINE_JUDGE
  22. auto duration = clock_t::now() - start_time;
  23.  
  24. typedef chrono::milliseconds time_scale;
  25.  
  26. auto elapsed_time = chrono::duration_cast< time_scale >( duration ).count();
  27.  
  28. cout << "void " << s << "() used " << elapsed_time << " ms\n";
  29. #endif // ONLINE_JUDGE
  30. }
  31.  
  32. #define time__( procedure ) time_monitor( #procedure, procedure )
  33. /// The code ends here
  34.  
  35. /// An example
  36.  
  37. const int N = 1e7; int a[ N ];
  38.  
  39. void procedure_1()
  40. {
  41. cout << "procedure 1" << endl;
  42.  
  43. for( int i = 0; i < N; i++ )
  44. a[ i ] = i;
  45. }
  46.  
  47. void procedure_2()
  48. {
  49. cout << "procedure 2" << endl;
  50.  
  51. for( int i = 0; i < N; i++ )
  52. a[ i ] = -i;
  53. }
  54.  
  55. int main()
  56. {
  57. ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );
  58.  
  59. time__( procedure_1 );
  60.  
  61. time__( procedure_2 );
  62. }
  63.  
Success #stdin #stdout 0s 54296KB
stdin
Standard input is empty
stdout
procedure 1
void procedure_1() used 8 ms
procedure 2
void procedure_2() used 4 ms