Advertisement
Guest User

hththt

a guest
Mar 25th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #
  2. include < stdio.h > #include < time.h > #include < stdlib.h > #include < sys / time.h > #include < sys / resource.h > #include < unistd.h > #include < sys / mman.h >
  3.   int main(int argc, char ** argv) {
  4.     struct timeval tv;
  5.     struct timezone tz;
  6.     int i, j, index, iterations, array_size;
  7.     float * arrayPtr;
  8.     double stop, start, init;
  9.     // Lock down all memory for this process
  10.     if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
  11.       fprintf(stderr, "Memlock error\n");
  12.       exit(1);
  13.     }
  14.     if (argc != 3) {
  15.       fprintf(stderr, "Usage: %s <Size of array><no of iterations>\n", argv[0]);
  16.       exit(1);
  17.     }
  18.     // progname=argv[0];
  19.     array_size = atoi(argv[1]);
  20.     iterations = atoi(argv[2]);
  21.     // Record start
  22.     gettimeofday( & tv, & tz);
  23.     init = tv.tv_sec + tv.tv_usec * 0.000001;
  24.     6
  25.     // create large float arrays
  26.     arrayPtr = calloc(array_size, sizeof(float));
  27.     //initialise array
  28.     for (i = 0; i < array_size; ++i) {
  29.       arrayPtr[i] = 9999.99;
  30.     }
  31.     // Main loop
  32.     // Process full array and time each pass.. Use random number
  33.     // generator to access array randomly .. greater likelihood of paging in unlocked version
  34.     for (j = 0; j < iterations; ++j) {
  35.       gettimeofday( & tv, & tz);
  36.       start = tv.tv_sec + tv.tv_usec * 0.000001;
  37.       // random seed
  38.       srand(time(NULL));
  39.       for (i = 0; i < array_size; ++i) {
  40.         // generate random index
  41.         index = rand() % array_size;
  42.         arrayPtr[index] = arrayPtr[index] / 0.9999;
  43.       }
  44.       gettimeofday( & tv, & tz);
  45.       stop = tv.tv_sec + tv.tv_usec * 0.000001;
  46.       printf("Run time for pass %d is %lf ms\n", j + 1, (stop - start) * 1000);
  47.       // sleep for 10 sec
  48.       sleep(10);
  49.       // Run multiple versions so unlocked versions may be paged during
  50.       // sleep period between passes
  51.     }
  52.     // release memory
  53.     free(arrayPtr);
  54.     munlockall(); // unlock memory
  55.     gettimeofday( & tv, & tz);
  56.     stop = tv.tv_sec + tv.tv_usec * 0.000001;
  57.     printf("Total Run time is %lf ms\n", (stop - init) * 1000);
  58.     return 0;
  59.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement