rjcostales

test_square.c

Feb 4th, 2021 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. /*******************************************************************************
  2.  
  3.    test_square
  4.  
  5.    Timed loops to compare the floating point calculation using f * f directly,
  6.    as an inline and regular function compared to using the intrinsic math pow()
  7.    function.  
  8.      
  9. *******************************************************************************/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <math.h>
  14.  
  15. #define LOOP 10000
  16. #define SIZE 10000
  17.  
  18. __attribute__((always_inline))
  19. inline static float inline_sq(float n) { return n * n; }
  20.  
  21. float square(float n) { return n * n; }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.    float reals[SIZE];
  26.    float result = 0.0;
  27.    double dummy;
  28.    clock_t start, stop;
  29.  
  30.    printf("%s\t%'dx%'d\n", argv[0], LOOP, SIZE);
  31.  
  32.    // random array of floats 1 & 10
  33.    for (int i = 0; i < SIZE; i++)
  34.       reals[i] = 1 + 9 * (float) rand() / (float) RAND_MAX;
  35.  
  36.    // test multiplication
  37.    start = clock();
  38.    dummy = 0;
  39.    for (int t = 0; t < LOOP; t++) {
  40.       result = 0;
  41.       for (int i = 0; i < SIZE; i++) {
  42.          float f = reals[i];
  43.          result += f * f;
  44.       }
  45.       dummy += result;
  46.    }
  47.    stop = clock();
  48.    printf("f * f\t\t%0.6f\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC, dummy);
  49.  
  50.    // test function inline
  51.    start = clock();
  52.    dummy = 0;
  53.    for (int t = 0; t < LOOP; t++) {
  54.       result = 0;
  55.       for (int i = 0; i < SIZE; i++) {
  56.          float f = reals[i];
  57.          result += inline_sq(f);
  58.       }
  59.       dummy += result;
  60.    }
  61.    stop = clock();
  62.    printf("inline_sq(f)\t%0.6f\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC, dummy);
  63.  
  64.    // test function square
  65.    start = clock();
  66.    dummy = 0;
  67.    for (int t = 0; t < LOOP; t++) {
  68.       result = 0;
  69.       for (int i = 0; i < SIZE; i++) {
  70.          float f = reals[i];
  71.          result += square(f);
  72.       }
  73.       dummy += result;
  74.    }
  75.    stop = clock();
  76.    printf("square(f, 2)\t%0.6f\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC, dummy);
  77.  
  78.    // test pow()
  79.    start = clock();
  80.    dummy = 0;
  81.    for (int t = 0; t < LOOP; t++) {
  82.       result = 0;
  83.       for (int i = 0; i < SIZE; i++) {
  84.          float f = reals[i];
  85.          result += pow(f, 2);
  86.       }
  87.       dummy += result;
  88.    }
  89.    stop = clock();
  90.    printf("pow(f, 2)\t%0.6f\t%0.6f\n", (float) (stop - start) / (float) CLOCKS_PER_SEC, dummy);
  91.  
  92. } /* main */
  93.  
Add Comment
Please, Sign In to add comment