Guest User

Untitled

a guest
Nov 30th, 2011
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #define _XOPEN_SOURCE
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <sys/time.h>
  6.  
  7. static const int N = 3 * 1000 * 1000;
  8.  
  9. double addem_accum(double* arr, int len, double sum) {
  10.     if (len <= 0) return sum;
  11.     else return addem_accum(arr + 1, len - 1, sum + *arr);
  12. }
  13.  
  14. double addem(double* arr, int len) {
  15.     return addem_accum(arr, len, 0.0);
  16. }
  17.  
  18. int main(int argc, char** argv) {
  19.     int i, j;
  20.     double* arr;
  21.     double result;
  22.     struct timeval tvstart, tvend;
  23.     srand48(35947);
  24.     arr = malloc(N * sizeof(double));
  25.     for (i = 0; i < N; i++) {
  26.         arr[i] = drand48();
  27.     }
  28.  
  29.     gettimeofday(&tvstart, NULL);
  30.     for (i = 0; i < 100; i++) {
  31.         result = addem(arr, N);
  32.     }
  33.     gettimeofday(&tvend, NULL);
  34.  
  35.     uint64_t micros = (tvend.tv_sec - tvstart.tv_sec) * 1000000 +
  36.         (tvend.tv_usec - tvstart.tv_usec);
  37.     printf("[%lf] %lf per iteration on average.\n", result, micros / 1.0e8);
  38. }
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment