Advertisement
rjcostales

math_ops.c

Sep 5th, 2021
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5.  
  6. #include <math.h>
  7.  
  8. #define LOOP 10000
  9. #define SIZE 10000
  10.  
  11. typedef double real;
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     real reals[SIZE];
  16.     real result = 0;
  17.  
  18.     clock_t start, stop;
  19.  
  20.     setlocale(LC_NUMERIC, "");
  21.  
  22.     printf("%s\t%'d - %'d\n", argv[0], LOOP, SIZE);
  23.  
  24.     for (register int i = 0; i < SIZE; i++)
  25.         reals[i] = (float) rand() / (float) rand();
  26.  
  27.     start = clock();
  28.     for (register int t = 0; t < LOOP; t++)
  29.         for (register int i = 0; i < SIZE; i++)
  30.             result = reals[i];
  31.     stop = clock();
  32.     printf("nop\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  33.  
  34.     start = clock();
  35.     for (register int t = 0; t < LOOP; t++)
  36.         for (register int i = 0; i < SIZE; i++)
  37.             result = sqrt(reals[i]);
  38.     stop = clock();
  39.     printf("sqrt\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  40.  
  41.     start = clock();
  42.     for (register int t = 0; t < LOOP; t++)
  43.         for (register int i = 0; i < SIZE; i++)
  44.             result = sin(reals[i]);
  45.     stop = clock();
  46.     printf("sin\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  47.  
  48.     start = clock();
  49.     for (register int t = 0; t < LOOP; t++)
  50.         for (register int i = 0; i < SIZE; i++)
  51.             result = cos(reals[i]);
  52.     stop = clock();
  53.     printf("cos\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  54.  
  55.     start = clock();
  56.     for (register int t = 0; t < LOOP; t++)
  57.         for (register int i = 0; i < SIZE; i++)
  58.             result = tan(reals[i]);
  59.     stop = clock();
  60.     printf("tan\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  61.  
  62.     start = clock();
  63.     for (register int t = 0; t < LOOP; t++)
  64.         for (register int i = 0; i < SIZE; i++)
  65.             result = atan(reals[i]);
  66.     stop = clock();
  67.     printf("atan\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  68.  
  69.     start = clock();
  70.     for (register int t = 0; t < LOOP; t++)
  71.         for (register int i = 0; i < SIZE; i++)
  72.             result = exp(reals[i]);
  73.     stop = clock();
  74.     printf("exp\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  75.  
  76.     start = clock();
  77.     for (register int t = 0; t < LOOP; t++)
  78.         for (register int i = 0; i < SIZE; i++)
  79.             result = log(reals[i]);
  80.     stop = clock();
  81.     printf("log\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  82.  
  83.     start = clock();
  84.     for (register int t = 0; t < LOOP; t++)
  85.         for (register int i = 0; i < SIZE; i++)
  86.             result = log10(reals[i]);
  87.     stop = clock();
  88.     printf("log10\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC);
  89. } /* main */
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement