By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

Which max() is faster?

P: n/a
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?
Dec 10 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <fj**********@reader01.singnet.com.sg>,
"Sing" <si**@gmail.comwrote:
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
Dec 10 '07 #2

P: n/a
On Dec 10, 6:21 pm, "Sing" <s...@gmail.comwrote:
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.
Dec 10 '07 #3

P: n/a
Khookie wrote:
On Dec 10, 6:21 pm, "Sing" <s...@gmail.comwrote:
>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;"
They should not compile dow to exactly the same code if a or b are
expressions with side effects.
Dec 10 '07 #4

P: n/a
"Sing" <si**@gmail.comwrote in
news:fj**********@reader01.singnet.com.sg:
1. #define max(a,b) (((a) (b)) ? (a) : (b))

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;
You might find this article on branching helpful:
http://en.wikipedia.org/wiki/Branch_...ter_science%29

--
Toms hilidhe
Dec 10 '07 #5

P: n/a
"Tom��������������� ���������������� " wrote:
"Sing" <si**@gmail.comwrote in
news:fj**********@reader01.singnet.com.sg:
>1. #define max(a,b) (((a) (b)) ? (a) : (b))


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;
Dec 10 '07 #6

P: n/a
Philip Potter <pg*@doc.ic.ac.ukwrote in news:fj**********@aioe.org:
> 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;


:-D

--
Toms hilidhe
Dec 10 '07 #7

P: n/a
On Dec 10, 12:21 am, "Sing" <s...@gmail.comwrote:
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).
Dec 10 '07 #8

P: n/a
Khookie wrote:
"Sing" <s...@gmail.comwrote:
>>
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;"
They should not create the same code. 2 (as modified) will exit
the function. 1 will select a value in an expression.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 10 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.