Advertisement
Guest User

pointer vs integer indexing

a guest
Dec 19th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <chrono>
  4. #include <unistd.h>
  5. #include <functional>
  6.  
  7. int main(void) {
  8.  
  9.   const int n{15000};
  10.   int* p{new int[n]};
  11.  
  12.     int * ep{ p + n };
  13.  
  14.   auto rand2 = std::bind(std::uniform_int_distribution<>(7,102), std::mt19937(std::random_device()()));
  15.  
  16.     for(int * q{p}; q!=ep; ++q )
  17.     {
  18.     *q = rand2();
  19.     }
  20.  
  21.   for (int j{0}; j < 20; j++) {
  22.     {
  23.  
  24.       auto start_pointer = std::chrono::high_resolution_clock::now();
  25.       for (int* q{p}; q != ep; ++q) {
  26.         *q +=1;
  27.       }
  28.       auto end_pointer = std::chrono::high_resolution_clock::now();
  29.       auto time_pointer = std::chrono::duration_cast<std::chrono::nanoseconds>(
  30.                               end_pointer - start_pointer)
  31.                               .count();
  32.       std::cout << " Pointer: " << time_pointer << std::endl;
  33.     }
  34.  
  35.     {
  36.       auto start_pointer = std::chrono::high_resolution_clock::now();
  37.       for (int* q{p}; q != ep; ++q) {
  38.         *q += 1;
  39.       }
  40.       auto end_pointer = std::chrono::high_resolution_clock::now();
  41.       auto time_pointer = std::chrono::duration_cast<std::chrono::nanoseconds>(
  42.                               end_pointer - start_pointer)
  43.                               .count();
  44.       std::cout << " Pointer: " << time_pointer << std::endl;
  45.     }
  46.  
  47.     {
  48.       auto start_index_32 = std::chrono::high_resolution_clock::now();
  49.       for (int i{0}; i != n; i++) {
  50.         p[i] += 1;
  51.       }
  52.       auto end_index_32 = std::chrono::high_resolution_clock::now();
  53.       auto time_index_32 = std::chrono::duration_cast<std::chrono::nanoseconds>(
  54.                                end_index_32 - start_index_32)
  55.                                .count();
  56.       std::cout << "Index 32: " << time_index_32 << std::endl;
  57.     }
  58.  
  59.     {
  60.       auto start_index_32 = std::chrono::high_resolution_clock::now();
  61.       for (int i{0}; i != n; i++) {
  62.         p[i] += 1;
  63.       }
  64.       auto end_index_32 = std::chrono::high_resolution_clock::now();
  65.       auto time_index_32 = std::chrono::duration_cast<std::chrono::nanoseconds>(
  66.                                end_index_32 - start_index_32)
  67.                                .count();
  68.       std::cout << "Index 32: " << time_index_32 << std::endl;
  69.     }
  70.  
  71.     {
  72.       const std::size_t n_64{n};
  73.       auto start_index_64 = std::chrono::high_resolution_clock::now();
  74.       for (std::size_t i{0}; i != n_64; i++) {
  75.         p[i] += 1;
  76.       }
  77.       auto end_index_64 = std::chrono::high_resolution_clock::now();
  78.       auto time_index_64 = std::chrono::duration_cast<std::chrono::nanoseconds>(
  79.                                end_index_64 - start_index_64)
  80.                                .count();
  81.       std::cout << "Index 64: " << time_index_64 << std::endl;
  82.     }
  83.  
  84.     {
  85.       const std::size_t n_64{n};
  86.       auto start_index_64 = std::chrono::high_resolution_clock::now();
  87.       for (std::size_t i{0}; i != n_64; i++) {
  88.         p[i] += 1;
  89.       }
  90.       auto end_index_64 = std::chrono::high_resolution_clock::now();
  91.       auto time_index_64 = std::chrono::duration_cast<std::chrono::nanoseconds>(
  92.                                end_index_64 - start_index_64)
  93.                                .count();
  94.       std::cout << "Index 64: " << time_index_64 << std::endl;
  95.     }
  96.     std::cout << std::endl;
  97.   }
  98.  
  99.   delete[] p;
  100.  
  101.   return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement