DarkShadow44

Untitled

Jan 25th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. int main(void)
  5. {
  6.     int** table = new int*[1000];
  7.     for(int i=0; i<1000; i++)
  8.         table[i]= new int[1000];
  9.  
  10.     for(int a=0; a<1000; a++)
  11.     {
  12.         for(int b=0; b<1000; b++)
  13.         {
  14.             table[a][b] = a*64 + b;
  15.         }
  16.     }
  17.  
  18.     int TEST;
  19.     std::cout << "Loops: ";
  20.     std::cin >> TEST;
  21.  
  22.     //Method 1
  23.     int x1=0;
  24.     time_t time1Start= time(0);
  25.     for(int i=0; i<TEST; i++)
  26.     {
  27.         for(int a=0; a< 1000; a++)
  28.         {
  29.             for(int b=0; b<1000; b++)
  30.             {
  31.                 x1+= a*64 + b;
  32.             }
  33.         }
  34.     }
  35.     time_t time1End= time(0);
  36.     double time1= difftime(time1End, time1Start);
  37.  
  38.     //Method 2
  39.     int x2=0;
  40.     time_t time2Start= time(0);
  41.     for(int i=0; i<TEST; i++)
  42.     {
  43.         for(int a=0; a< 1000; a++)
  44.         {
  45.             for(int b=0; b<1000; b++)
  46.             {
  47.                 //x2+= table[b][a]; //very slow
  48.                 x2+= table[a][b];
  49.             }
  50.         }
  51.     }
  52.     time_t time2End= time(0);
  53.     double time2= difftime(time2End, time2Start);
  54.  
  55.     std::cout << std::endl;
  56.     std::cout << "x1: " << x1 << " time1: " << time1 <<std::endl;
  57.     std::cout << "x2: " << x2 << " time2: " << time2 <<std::endl;
  58.  
  59.     system("pause");
  60. }
Advertisement
Add Comment
Please, Sign In to add comment