Advertisement
Jakowlew

Untitled

Aug 15th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define ARR_SIZE 1'000'000
  7. #define TEST_COUNT 1'000
  8.  
  9. const int* create_array(int n);
  10. int arrmax(const int* arr, int length);
  11.  
  12. int main()
  13. {
  14.     srand(time(NULL));
  15.  
  16.     const int* arr = create_array(ARR_SIZE);
  17.  
  18.     clock_t t;
  19.     t = clock();
  20.     for (int i = 0; i < TEST_COUNT; ++i)
  21.     {
  22.         volatile int imax = arrmax(arr, ARR_SIZE);
  23.     }
  24.     t = clock() - t;
  25.     double time_taken = ((double)t) / CLOCKS_PER_SEC;
  26.  
  27.     printf("Elapsed time: %fs",  time_taken / TEST_COUNT);
  28. }
  29.  
  30. const int* create_array(int n)
  31. {
  32.     int* arr = (int*)malloc(n * sizeof(n));
  33.     for (int i = 0; i < n; ++i)
  34.         arr[i] = rand();
  35.     return arr;
  36. }
  37.  
  38. int arrmax(const int* arr, int length)
  39. {
  40.     int max = INT32_MIN;
  41.     for (int i = 0; i < length; ++i)
  42.         if (arr[i] > max)
  43.             max = arr[i];
  44.     return max;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement