Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <time.h>
- #define MAX 100000
- double A[MAX];
- static double sinatan(double x)
- {
- return sin(atan(x));
- }
- static double cosatan(double x)
- {
- return cos(atan(x));
- }
- static double sinatan2(double x)
- {
- return x / sqrt(x*x + 1);
- }
- static double cosatan2(double x)
- {
- return 1./ sqrt(x*x + 1);
- }
- static void generate_random_array(int n, double a[])
- {
- int i;
- for (i = 0; i < n; ++i)
- a[i] = (double) rand() / (double) RAND_MAX;
- }
- int main()
- {
- struct timespec start, end;
- unsigned long long int delta_t;
- int i, j;
- volatile double y;
- generate_random_array(MAX, A);
- //Initialize timespec structures.
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- clock_gettime(CLOCK_MONOTONIC_RAW, &end);
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- for (i = 0; i < MAX; ++i)
- {
- y = sinatan2(A[i]);
- }
- clock_gettime(CLOCK_MONOTONIC_RAW, &end);
- delta_t = end.tv_nsec - start.tv_nsec;
- printf("time x/sqrt(x*x + 1) = %llu\n", delta_t);
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- for (i = 0; i < MAX; ++i)
- {
- y = sinatan(A[i]);
- }
- clock_gettime(CLOCK_MONOTONIC_RAW, &end);
- delta_t = (end.tv_nsec - start.tv_nsec);
- printf("time sin(atan(x)) = %llu\n", delta_t);
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- for (i = 0; i < MAX; ++i)
- {
- y = cosatan2(A[i]);
- }
- clock_gettime(CLOCK_MONOTONIC_RAW, &end);
- delta_t = end.tv_nsec - start.tv_nsec;
- printf("time 1/sqrt(x*x + 1) = %llu\n", delta_t);
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- for (i = 0; i < MAX; ++i)
- {
- y = cosatan(A[i]);
- }
- clock_gettime(CLOCK_MONOTONIC_RAW, &end);
- delta_t = (end.tv_nsec - start.tv_nsec);
- printf("time cos(atan(x)) = %llu\n", delta_t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement