473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Which max() is faster?

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
8 3363
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
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
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
"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

--
Tomás Ó hÉilidhe
Dec 10 '07 #5
"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
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

--
Tomás Ó hÉilidhe
Dec 10 '07 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: junky_fellow | last post by:
which of the following is faster and why ? if ( x == 1 ) { } or if ( x != 0 ) {
9
by: niteshs | last post by:
Hello all, just checking which method is faster string A; A = ""; if (A == string.Empty) { MessageBox.Show("a is empty"); } else
5
by: Brian Henry | last post by:
Which is faster or considered the better way to do this. I have a object that holds a date... Convert.ToDate(object) or Directcast(object,DateTime) ? Thanks
11
by: Farel | last post by:
Which is Faster in Python and Why? jc = {}; m = x = ,,.......] # upwards of 10000 entries def mcountb(): for item in x: b = item; b.sort(); bc = 0 for bitem in b: bc += int(bitem) try: m...
4
by: shuisheng | last post by:
Dear all, Would you please tell me when using pointer and not using pointer, which is faster to access data? Such as float *pVal float val MyClass *pA pA->member1 MyClass a ...
4
by: Sonnich | last post by:
Hi I have a costum function for a special search, which sort strings. This is currently the place where I can save a lot of time (~70%) if possible. So, which is faster: for($j =...
3
by: vozzek | last post by:
Hi everyone, quick question: When I first started my website three months ago, I was a total rookie. I used a lot of <img> tags to create borders around things and to dress up different tables on...
36
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.