-5

Code 1

int A, B, MAX;
cout << "Give two numbers:" << endl;
cin >> A >> B;

if (A > B)
{ 
  MAX = A;
}
else 
{
  MAX = B;
}
cout << "Largest amongst given numbers is: ";
cout << MAX << endl;
return 0;

Code 2

int A, B, MAX;
cout << "Give two numbers:" << endl;
cin >> A >> B;

MAX = A;
if (B > MAX)
{
  MAX = B;
}
cout << "Largest amongst given numbers is: ";
cout << MAX << endl;
return 0;

In above program logic, which one is best and why? is there any difference between them.? it is my exam question i would like to ask stack overflow to know best opinion.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • 5
    Why not `MAX = std::max(A, B);`? – user2357112 Mar 29 '14 at 03:56
  • 1
    What's your definition of best? – user1118321 Mar 29 '14 at 03:57
  • Which of the two programs is better and why? – user3474853 Mar 29 '14 at 03:59
  • Since the answer is rather arbitrary I would guess this specific case is covered in the course material for your class. I've seen option 2 described as an optimization many years ago although with a modern compiler I'd bet option 1 is more efficient today. – Retired Ninja Mar 29 '14 at 04:44
  • 1
    Don't change your question to something completely different. If you have a different question, make another post. But don't bother in this case, that one will immediately be closed as opinion based. Please read the [help section](http://stackoverflow.com/help) before continuing to use this site. – Benjamin Lindley Mar 29 '14 at 04:59

4 Answers4

5
MAX = std::max(A, B);

Is better than both in terms of clarity.

In terms of speed, the compiler should be able to optimise any of these methods to be equivalent; but again I'd favour std::max because I'd sooner trust the compiler to optimise a standard function than some arbitrary made up code to perform the same task.

karadoc
  • 2,641
  • 22
  • 21
1

They are the same so I would prefer code 1 because it's more readable. In both cases you have to bring both A and B into a register (regardless) and then do a single comparison (regardless). And it won't write out to the variable MAX until after this segment is done (because it won't need to kick anything out of a register).

This isn't something that is going to cause any kind of performance gain. In fact, it's possible (although I doubt it) that the compiler would compile them the same (the compiler does all kinds of code modification to create an optimal set of instructions).

As suggested the only thing that likely would give a performance gain is using the library function std::max. This is because the compiler will basically perform the comparison in the most efficient way (likely without even calling a conditional jump). If your two values are integers, then you can see here that it's possible to find the max with five integer operations (all of which, except the multiplication can generally be done in a single cycle). You generally want to avoid conditional jumps as much as possible and this algorithm does that. Most likely the max function does something like this or similar (but it would have to be different for floating point values).

Community
  • 1
  • 1
Jared
  • 940
  • 5
  • 9
1

After MAX = std::max(A,B), the next best code is:

MAX = A > B ? A : B;

If you don't want to use that, then I prefer your "code 1" because:

  • code 2 always sets MAX = A, which is momentarily misleading and only becomes clear as the later code is studied; while this issue is common in C++ and many other languages, there's no particular reason to embrace that complication when it's easily avoided

  • for some types (the question didn't originally specify int), an assignment may be an expensive operation (e.g. copying a lot of data), so always assigning once is better than potentially assigning twice

For both those reasons, it also desirable to declare and define MAX in one step. It may not be practical if say it's a non-const reference accepted as a function argument, but when it is possible it's another good reason to prefer std::max or the ternary ? approach: you won't need a misleading and potentially inefficient or unavailable default construction. (Now you've changed the question to be int specific, the expense of copying and construction is known and trivial, and the optimiser can be counted on to remove them in most cases).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • Setting max twice in this particular example shouldn't actually cause two writes (it could if you have a horrible compiler). The only way this could happen is if the type is some complicated `struct` or object and the `<` operator is some complicated function. Still, the compiler should be smart enough to realize it shouldn't write out to `MAX` until it's done the comparison of `A` and `B`. So it should recognize `MAX` as an alias for `A` and just load `A` to perform the comparison (which would basically change this to code 1). – Jared Mar 29 '14 at 19:35
  • 1
    @Jared much of what I say about efficiency predates the question being edited to say A and B are integers. Say they're strings, then your "it should recognise MAX as an alias" sounds good but isn't a legal optimisation if it removes side effects, such as when B.size() > A.capacity() – Tony Delroy Mar 30 '14 at 00:22
0

I would say use code 2. It is better because you explicitly say that if MAX B is greater than MAX A, then change MAX to B. In the other one, you don't have any defining factors about why MAX A is greater than MAX B. From what I see, you will probably have a harder time using code 1 than code 2.

  • 3
    I would disagree; the first seems pretty clear to me. If A is greater than B, A is the maximum. Otherwise, B is the maximum. Aside from answers invoking `std::max`, this whole question seems largely opinion-based to me. – cf- Mar 29 '14 at 04:07