Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <time.h>
- #define ARR_SIZE 1'000'000
- #define TEST_COUNT 1'000
- const int* create_array(int n);
- int arrmax(const int* arr, int length);
- int main()
- {
- srand(time(NULL));
- const int* arr = create_array(ARR_SIZE);
- clock_t t;
- t = clock();
- for (int i = 0; i < TEST_COUNT; ++i)
- {
- volatile int imax = arrmax(arr, ARR_SIZE);
- }
- t = clock() - t;
- double time_taken = ((double)t) / CLOCKS_PER_SEC;
- printf("Elapsed time: %fs", time_taken / TEST_COUNT);
- }
- const int* create_array(int n)
- {
- int* arr = (int*)malloc(n * sizeof(n));
- for (int i = 0; i < n; ++i)
- arr[i] = rand();
- return arr;
- }
- int arrmax(const int* arr, int length)
- {
- int max = INT32_MIN;
- for (int i = 0; i < length; ++i)
- if (arr[i] > max)
- max = arr[i];
- return max;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement