fork download
  1. #include <sys/time.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define MAX_TIMER_NUM 256
  6. static double s_tmptime[MAX_TIMER_NUM] = {}; // [s]
  7. void stopwatch_start(int index)
  8. {
  9. if (index >= MAX_TIMER_NUM) {
  10. cout << "INVALID TIMER NUM, IGNORED: MAX_TIMER_NUM is " << MAX_TIMER_NUM<< endl;
  11. return;
  12. }
  13. struct timeval tim;
  14. gettimeofday(&tim, NULL);
  15. s_tmptime[index] = tim.tv_sec+(tim.tv_usec / 1000000.0);
  16. }
  17.  
  18. double stopwatch_end(int index)
  19. {
  20. if (index >= MAX_TIMER_NUM) {
  21. cout << "INVALID TIMER NUM, IGNORED: MAX_TIMER_NUM is " << MAX_TIMER_NUM<< endl;
  22. return -1;
  23. }
  24. struct timeval tim;
  25. gettimeofday(&tim, NULL);
  26. return tim.tv_sec+(tim.tv_usec / 1000000.0) - s_tmptime[index];
  27. }
  28.  
  29. double get_clock_now(void)
  30. {
  31. struct timeval tv;
  32. gettimeofday(&tv, NULL);
  33. return tv.tv_sec + (double)tv.tv_usec*1e-6;
  34. }
  35.  
  36.  
  37. #define rep(i,n) for(int i = 0; i < n; i++)
  38. #define ll long long
  39. vector<ll> a;
  40. vector<ll> b;
  41. int main(void) {
  42. for (ll cycle = 1; cycle <= 10000000; cycle *= 10) {
  43. cout << cycle << " ";
  44.  
  45. {
  46. stopwatch_start(0);
  47. rep(i, cycle) {
  48. a.push_back(i);
  49. }
  50. double t = stopwatch_end(0);
  51. cout << t << " ";
  52. a.clear();
  53. }
  54.  
  55. {
  56. b.reserve(cycle);
  57. stopwatch_start(0);
  58. rep(i, cycle) {
  59. b.push_back(i);
  60. }
  61. double t = stopwatch_end(0);
  62. cout << t << " ";
  63. b.clear();
  64. }
  65. cout << endl;
  66. }
  67.  
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0.55s 3568KB
stdin
Standard input is empty
stdout
1 1.93657e-06 9.9407e-07 
10 9.91812e-06 1.03668e-06 
100 1.29824e-05 2.00153e-06 
1000 3.99676e-05 2.2085e-05 
10000 0.000570024 0.000163998 
100000 0.00524106 0.002793 
1000000 0.0243821 0.016225 
10000000 0.328684 0.160289