Advertisement
Guest User

cosatan and sinatan performance benchmark

a guest
Aug 2nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. #include <sys/time.h>
  6. #include <time.h>
  7.  
  8. #define MAX 100000
  9. double A[MAX];
  10.  
  11. static double sinatan(double x)
  12. {
  13.     return sin(atan(x));
  14. }
  15.  
  16. static double cosatan(double x)
  17. {
  18.     return cos(atan(x));
  19. }
  20.  
  21. static double sinatan2(double x)
  22. {
  23.     return x / sqrt(x*x + 1);
  24. }
  25.  
  26. static double cosatan2(double x)
  27. {
  28.     return 1./ sqrt(x*x + 1);
  29. }
  30.  
  31.  
  32. static void generate_random_array(int n, double a[])
  33. {
  34.     int i;
  35.  
  36.     for (i = 0; i < n; ++i)
  37.         a[i] = (double) rand() / (double) RAND_MAX;
  38. }
  39.  
  40. int main()
  41. {
  42.     struct timespec start, end;
  43.     unsigned long long int delta_t;
  44.     int i, j;
  45.     volatile double y;
  46.  
  47.     generate_random_array(MAX, A);
  48.  
  49.    
  50.     //Initialize timespec structures.
  51.     clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  52.     clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  53.    
  54.     clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  55.     for (i = 0; i < MAX; ++i)
  56.     {
  57.         y = sinatan2(A[i]);
  58.     }
  59.     clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  60.     delta_t = end.tv_nsec - start.tv_nsec;
  61.     printf("time x/sqrt(x*x + 1) = %llu\n", delta_t);
  62.  
  63.     clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  64.     for (i = 0; i < MAX; ++i)
  65.     {
  66.         y = sinatan(A[i]);
  67.     }
  68.     clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  69.     delta_t = (end.tv_nsec - start.tv_nsec);
  70.     printf("time sin(atan(x))    = %llu\n", delta_t);    
  71.  
  72.     clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  73.     for (i = 0; i < MAX; ++i)
  74.     {
  75.         y = cosatan2(A[i]);
  76.     }
  77.     clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  78.     delta_t = end.tv_nsec - start.tv_nsec;
  79.     printf("time 1/sqrt(x*x + 1) = %llu\n", delta_t);
  80.  
  81.     clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  82.     for (i = 0; i < MAX; ++i)
  83.     {
  84.         y = cosatan(A[i]);
  85.     }
  86.     clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  87.     delta_t = (end.tv_nsec - start.tv_nsec);
  88.     printf("time cos(atan(x))    = %llu\n", delta_t);    
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement