To get the maximum of two numbers, it seems common practice to define it like this:
Code:
#define MAX(a,b) ((a)>(b)?(a):(b))
For my project, I need to repeatedly find the maximum of 2-5 numbers. And I want to do it efficient as it will be used billions of times.
The time I call, I know which case I need, so I could define functions for each case.

I saw people using this:
Code:
#define MAX4(a,b,c,d) (MAX(a,b) > MAX(c,d) ? MAX(a,b) : MAX(c,d))
But then I need 4 comparisons (which could be done in 3).


So, would it make sense to have functions max3(a,b,c), max4(a,b,c,d) and max5(a,b,c,d,e) like the following?
Code:
int max4(int a, int b, int c, int d) {
  int max =a;
  if (b>max) {max = b;}
  if (c>max) {max = c;}
  if (d>max) {max = d;}
  return max;
}
Alternatively do the same row of computations in the main function itself, does it create any overhead to pass it to a function etc?


To get the maximum of several (abritrary large number of) numbers, one can use sth like:
Code:
int max(int *arr, int arr_size) {
  int max = *(arr+0);
  int i;
  for(i=1; i<arr_size; i++) {
    if(*(arr+i) > max) {
      max = *(arr+i);
    }
  }
  return max;
}
But to use such an approach, one needs to create an array to hold all the values first. Is it the same complexity as the one above?

The values for a,b,c do also have to be computed and I could call a max4(what-i-do-to-get-first-value,comp-for-2nd,...) which means I don't need to allocate the space in main first, will I gain anything by this approach? Or is it just less readable?

As you can see, I was thinking about it and I programmed quite a lot, but I am new to C, and need to get the concepts clear... I can think of many alternatives to the same problem, but I can not argue which one is 'smartest', fastest, least memory hungry, ... (which I know often are different answers!)