Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XOPEN_SOURCE
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <sys/time.h>
- static const int N = 3 * 1000 * 1000;
- double addem_accum(double* arr, int len, double sum) {
- if (len <= 0) return sum;
- else return addem_accum(arr + 1, len - 1, sum + *arr);
- }
- double addem(double* arr, int len) {
- return addem_accum(arr, len, 0.0);
- }
- int main(int argc, char** argv) {
- int i, j;
- double* arr;
- double result;
- struct timeval tvstart, tvend;
- srand48(35947);
- arr = malloc(N * sizeof(double));
- for (i = 0; i < N; i++) {
- arr[i] = drand48();
- }
- gettimeofday(&tvstart, NULL);
- for (i = 0; i < 100; i++) {
- result = addem(arr, N);
- }
- gettimeofday(&tvend, NULL);
- uint64_t micros = (tvend.tv_sec - tvstart.tv_sec) * 1000000 +
- (tvend.tv_usec - tvstart.tv_usec);
- printf("[%lf] %lf per iteration on average.\n", result, micros / 1.0e8);
- }
Advertisement
Add Comment
Please, Sign In to add comment