Which max() is faster?

S

Sing

Dear C Gurus,

I would like to optimise a max() algo that my program uses many times.
Which one is faster or are they the same?

1. #define max(a,b) (((a) > (b)) ? (a) : (b))
2. if (a>b) return a

I have a feeling that (1) is faster, but cannot explain why. Can some Gurus
enlighten?
 
F

Fancois Grieu

"Sing said:
Dear C Gurus,

I would like to optimise a max() algo that my program uses many times.
Which one is faster or are they the same?

1. #define max(a,b) (((a) > (b)) ? (a) : (b))
2. if (a>b) return a

I have a feeling that (1) is faster, but cannot explain why. Can some Gurus
enlighten?


Write an actual program. To get 2 working, you have to add something
that you do not need with 1. What is this something ? Is there any
overhead associated to it on your particular platform ?
Also, to get higher marks on this homework: give an example where 2
works, but not 1.

Francois Grieu
 
K

Khookie

Dear C Gurus,

I would like to optimise a max() algo that my program uses many times.
Which one is faster or are they the same?

1. #define max(a,b) (((a) > (b)) ? (a) : (b))
2. if (a>b) return a

I have a feeling that (1) is faster, but cannot explain why. Can some Gurus
enlighten?

i think u meant "if (a > b) return a else return b;" with no. 2.

they should both compile down to exactly the same code (with or
without optimisations). The (a ? b : c) ternary operator is a
shortcut for "if a return b else return c;"

But if in doubt, look at the disassembly.
 
J

James Kuyper

Khookie said:
i think u meant "if (a > b) return a else return b;" with no. 2.

they should both compile down to exactly the same code (with or
without optimisations). The (a ? b : c) ternary operator is a
shortcut for "if a return b else return c;"

They should not compile dow to exactly the same code if a or b are
expressions with side effects.
 
P

Philip Potter

Tom�������������������������������� said:
You're naive if you think the ternary operator won't result in CPU
instructions that cause branching just like an "if" statement. You might
get identical machine code from the following two:

i = a ? 7 : 2;

and:

if (i) a = 7;
else a = 2;

It's not very likely though. It's much more likely for these two to have
identical machine code:

a = i ? 7 : 2;

and:

if (i) a = 7;
else a = 2;
 
T

Tomás Ó hÉilidhe

Philip Potter said:
It's not very likely though. It's much more likely for these two to
have identical machine code:

a = i ? 7 : 2;

and:

if (i) a = 7;
else a = 2;



:-D
 
U

user923005

Dear C Gurus,

I would like to optimise a max() algo that my program uses many times.
Which one is faster or are they the same?

1. #define max(a,b) (((a) > (b)) ? (a) : (b))
2. if (a>b) return a

I have a feeling that (1) is faster, but cannot explain why. Can some Gurus
enlighten?

I can tell you that you are doing a bad thing worrying about which of
these two things is faster.
You should write the one that seems the most clear to you, or better
yet, use a system provided max().
The first rule of optimization is: "Don't do it."
The second rule of optimization (for experts only) is: "Don't do it
yet."

Seriously, it is a big mistake to write twiddly things that you think
are going to outsmart the compiler. The chances are very good that
you will:
1. Introduce bugs
2. Make something much harder to maintain
3. Make something that is not any faster anyway
4. Waste a huge amount of time trying to speedup something that does
not need to be sped up.

You should never optimize code without a profile session. You should
never run the profile session unless the code does not meet performace
specifications. It is almost certain that you will be optimizing in
the wrong place if you just willy-nilly start tweaking things. And
both of your proposed solutions are O(1) which means that at best the
performance difference will be a small, constant factor. Unless these
constructs are in a deeply nested loop, you will be unable to measure
the performance differece found by choosing one format over the other
(if, indeed, there is any performance difference).
 
C

CBFalconer

Khookie said:
i think u meant "if (a > b) return a else return b;" with no. 2.

they should both compile down to exactly the same code (with or
without optimisations). The (a ? b : c) ternary operator is a
shortcut for "if a return b else return c;"

They should not create the same code. 2 (as modified) will exit
the function. 1 will select a value in an expression.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top