Pella86

Test array accesss

Apr 11th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. #define SIZETOTEST 100000
  6.  
  7. int main (int argc, char * const argv[]) {
  8.    
  9.     std::vector<int> myvec;
  10.     int myarray[SIZETOTEST];
  11.    
  12.     for (int i = 0; i < SIZETOTEST; ++i) {
  13.         myvec.push_back(i);
  14.         myarray[i] = i;
  15.     }
  16.     typedef std::vector<int>::iterator IT_int;
  17.    
  18.     clock_t start,end;
  19.    
  20.     start = std::clock();
  21.     for (IT_int it = myvec.begin(); it != myvec.end(); ++it) {
  22.         *it += 1;
  23.     }
  24.     end = std::clock();
  25.    
  26.     std::cout << "iterator it:   " << end - start << std::endl;
  27.    
  28.     start = std::clock();
  29.     for (int i = 0; i < myvec.size(); ++i) {
  30.         myvec[i] += 1;
  31.     }
  32.     end = std::clock();
  33.    
  34.     std::cout << "int i:         "<< end - start << std::endl;
  35.    
  36.     start = std::clock();
  37.     for (int i = 0; i < SIZETOTEST; ++i) {
  38.         myarray[i] = myarray[i] + 1;
  39.     }
  40.     end = std::clock();
  41.     std::cout << "c-style array: " << end - start << std::endl;
  42.    
  43.     return 0;
  44. }
  45.  
  46. /*
  47. output: compiled with compiled with XCode 3.2.5, GCC 4.2
  48. in debug mode (no or really few optimization):
  49.  
  50. iterator it:   2430
  51. int i:         1141
  52. c-style array: 561
  53.  
  54. in release mode:
  55.  
  56. iterator it:   108
  57. int i:         160
  58. c-style array: 2
  59. */
Advertisement
Add Comment
Please, Sign In to add comment