Advertisement
Nickpips

Reserve Speeds

Jun 15th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     clock_t t1;
  10.     clock_t t2;
  11.    
  12.     t1 = clock();
  13.     for( int t = 0; t < 1000; t++ )
  14.     {
  15.         vector<int> a;
  16.         a.reserve(100000);
  17.         for( int i = 0; i < 100000; i++ )
  18.             a.push_back(i);
  19.     }
  20.     t2 = clock();
  21.    
  22.     cout << (t2-t1) / (double)CLOCKS_PER_SEC << endl;
  23.    
  24.     t1 = clock();
  25.     for( int t = 0; t < 1000; t++ )
  26.     {
  27.         vector<int> a;
  28.         for( int i = 0; i < 100000; i++ )
  29.             a.push_back(i);
  30.     }
  31.     t2 = clock();
  32.    
  33.     cout << (t2-t1) / (double)CLOCKS_PER_SEC << endl;
  34.    
  35.     return 0;
  36. }
  37.  
  38. /*
  39.  
  40. TESTED OUTPUT:
  41.  
  42. (with -O3)
  43. 0.27953
  44. 0.599311
  45.  
  46. (default)
  47. 1.56081
  48. 1.9982
  49.  
  50. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement