Advertisement
cd62131

macro vs function

Feb 27th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #define LOOP_LIMIT 10000
  5. #define MAX( a, b ) ((a) > (b) ? (a) : (b))
  6. int main(void) {
  7.   int i, j;
  8.   double x, y;
  9.   clock_t stime, etime;
  10.   stime = clock();
  11.   for (i = 0; i < LOOP_LIMIT; i++) {
  12.     for (j = 0; j < LOOP_LIMIT; j++) {
  13.       x = i * 1.0;
  14.       y = j * 2.0;
  15.       x = MAX(x, y);
  16.     }
  17.   }
  18.   etime = clock();
  19.   printf("MACRO Version : time = %g\n",
  20.     (double) (etime - stime) / CLOCKS_PER_SEC);
  21.  
  22.   stime = clock();
  23.   for (i = 0; i < LOOP_LIMIT; i++) {
  24.     for (j = 0; j < LOOP_LIMIT; j++) {
  25.       x = i * 1.0;
  26.       y = j * 2.0;
  27.       x = fmax(x, y);
  28.     }
  29.   }
  30.   etime = clock();
  31.   printf("FUNCTION Version : time = %g\n",
  32.     (double) (etime - stime) / CLOCKS_PER_SEC);
  33.   return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement