Be1ng_Kr1Sh
Be1ng_Kr1Sh

Reputation: 403

Which approach is better - More conditions or More Variables?

I had to write one function to get the largest among four numbers. I find two ways to do that -

#include <stdio.h>                                                        
int main() 
{
    int a, b, c, d;
    int max_of_four(int a, int b, int c, int d);
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
    return 0;
}

int max_of_four(int a,int b,int c,int d)
{
    int result,result2,result3;
    if(a>b)
        result=a;
    else
        result=b;
    if(result>c)
        result2=result;
    else
        result2=c;
    if(result2>d)
        result3=result2;
    else
        result3=d;
    return result3;
}

Or I can write the function like this also -

int max_of_four(int a,int b,int c,int d) 
{
    int greatest_int;
    if (a>b && a>c && a>d)
        greatest_int=a;
    else if (b>c && b>d)
        greatest_int=b;
    else if (c>d)
        greatest_int=c;
    else
        greatest_int=d;

    return greatest_int;
}

May I know which would be the better as in the first function I am using more variables and in the next one I am using more conditions. I tried running both & they are taking same amount of time so I couldn't differentiate between the two. As I am just starting with programming with C it would be good to know this as I go forward. Thank You.

Upvotes: 0

Views: 113

Answers (3)

4386427
4386427

Reputation: 44340

Which approach is better - More conditions or More Variables?

First you need to define what you mean by better

Is it better performance?

Is it less memory usage?

Is it better maintenance?

Guessing about performance by looking at the C code is something that you shouldn't do - especially when being new to C. The compiler makes all kinds of optimizations on your C code, so there is (nearly) no way to predict performance. The only solution is to profile.

The same apply to memory usage - even though you define some variables, the compiler is likely to optimize them away. You'll have to inspect the generated assembler code to get an answer.

Regarding maintenance - in nearly all cases this is where you should focus. Make sure that your code is easy to understand (and their by to maintain). Performance issues come second.

Let's look at this code:

int max_of_four(int a,int b,int c,int d)
{
    int result,result2,result3;
    if(a>b)
        result=a;
    else
        result=b;
    if(result>c)
        result2=result;
    else
        result2=c;
    if(result2>d)
        result3=result2;
    else
        result3=d;
    return result3;
}

Here you say that you worry about the number of variables...

Well let's rewrite the code - let's pretend I'm a compiler.

The first thing I notice is that once result is initialized, the variable a isn't used anymore. So why introduce a new variable result when I have a available already. So instead of result I simply use a and rewrite to:

int max_of_four(int a,int b,int c,int d)
{
    int result2,result3;
    if(a>b)
        a=a;
    else
        a=b;
    if(a>c)
        result2=a;
    else
        result2=c;
    if(result2>d)
        result3=result2;
    else
        result3=d;
    return result3;
}

Now the first if is rather strange, so I rewrite to:

int max_of_four(int a,int b,int c,int d)
{
    int result2,result3;
    if(b >= a) a=b;
    if(a>c)
        result2=a;
    else
        result2=c;
    if(result2>d)
        result3=result2;
    else
        result3=d;
    return result3;
}

Once again I notice that once result2 is initialized, the variable a isn't used anymore. So I can repeat the pattern from above and get rid of result2 by replacing it by a. After that I can repeat the same pattern to get rid of result3 and my code looks:

int max_of_four(int a,int b,int c,int d)
{
    if(b >= a) a = b;
    if(c >= a) a = c;
    if(d >= a) a = d;
    return a;
}

Still worried about the number of variables?

Since the compiler can see when the various variables are still in use (or no longer in use), the compiler may optimize your original code just like above by reusing "dead" variables.

But... the compiler will probably do something even more optimal. What? I don't know before I take a look at the generated assembly code.

So the conclusion is - don't look at the C code when finding the better way.

Upvotes: 3

Acorn
Acorn

Reputation: 26194

static int max(int a, int b)
{
    return (a > b) ? a : b;
}

int f(int a, int b, int c, int d)
{
    return max(max(max(a, b), c), d);
}

Upvotes: 1

Or you can write this:

int max_of_four(int a,int b,int c,int d) 
{
    int greatest_int = a;
    if (b > greatest_int) {
        greatest_int = b;
    }
    if (c > greatest_int) {
        greatest_int = c;
    }
    if (d > greatest_int) {
        greatest_int = d;
    }
    return greatest_int;
}

Or something like this...

int max_of_four(int a,int b,int c,int d) 
{
    int greatest_int = a;
    int *iter = (int[]){b, c, d}, *end = iter + 3;
    for (; iter < end; iter ++) {
        if (*iter > greatest_int) {
            greatest_int = *iter;
        }
    }
}

Upvotes: 4

Related Questions