jkavart

j

Feb 7th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. /* Objective: Exhaust the memory to understand fragmentation and explain the timings.
  6. *
  7. */
  8.  
  9. void main(int argc, char** argv)
  10. {
  11.     clock_t begin1, stop1, begin2, stop2;
  12.     double tdif = 0, tdif2 = 0;
  13.     for(int k = 0; k < 1000; k++)
  14.     {
  15.         double dif, dif2;
  16.         const int m = 480; //Change this
  17.         begin1 = clock();
  18.         printf("Step One\n"); //DO NOT CHANGE
  19.         int *container[3 * m];
  20.         for(int i = 0; i < (3 * m); i++)
  21.         {
  22.             int *tmpAry = (int *)malloc(800000 * sizeof (int));
  23.             container[i] = tmpAry;
  24.         }
  25.         stop1 = clock();
  26.         printf("Step Two\n"); //DO NOT CHANGE
  27.         for(int i = 0; i < (3 * m); i += 2)
  28.         {
  29.             free(container[i]);
  30.         }
  31.         begin2 = clock();
  32.         printf("Step Three\n\n"); //DO NOT CHANGE
  33.         //int *container2[m];
  34.         for(int i = 0; i < (3 * m); i += 2)
  35.         {
  36.             int *tmpAry = (int *)malloc(900000 * sizeof (int));
  37.             container[i] = tmpAry;
  38.         }
  39.         stop2 = clock();
  40.         dif = (stop1 - begin1) / 1000.00;
  41.         dif2 = (stop2 - begin2) / 1000.00;
  42.         tdif += dif;
  43.         tdif /= 2;
  44.         tdif2 += dif2;
  45.         tdif2 /= 2;
  46.     }
  47.     printf("First array allocation: %.5f\n", tdif);
  48.     printf("Second array allocation: %.5f\n", tdif2);
  49.     system("pause");
  50. };
Add Comment
Please, Sign In to add comment