Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- int main(void)
- {
- int** table = new int*[1000];
- for(int i=0; i<1000; i++)
- table[i]= new int[1000];
- for(int a=0; a<1000; a++)
- {
- for(int b=0; b<1000; b++)
- {
- table[a][b] = a*64 + b;
- }
- }
- int TEST;
- std::cout << "Loops: ";
- std::cin >> TEST;
- //Method 1
- int x1=0;
- time_t time1Start= time(0);
- for(int i=0; i<TEST; i++)
- {
- for(int a=0; a< 1000; a++)
- {
- for(int b=0; b<1000; b++)
- {
- x1+= a*64 + b;
- }
- }
- }
- time_t time1End= time(0);
- double time1= difftime(time1End, time1Start);
- //Method 2
- int x2=0;
- time_t time2Start= time(0);
- for(int i=0; i<TEST; i++)
- {
- for(int a=0; a< 1000; a++)
- {
- for(int b=0; b<1000; b++)
- {
- //x2+= table[b][a]; //very slow
- x2+= table[a][b];
- }
- }
- }
- time_t time2End= time(0);
- double time2= difftime(time2End, time2Start);
- std::cout << std::endl;
- std::cout << "x1: " << x1 << " time1: " << time1 <<std::endl;
- std::cout << "x2: " << x2 << " time2: " << time2 <<std::endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment