Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which max() is faster?

Reply
Thread Tools

Which max() is faster?

 
 
Sing
Guest
Posts: n/a
 
      12-10-2007
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?


 
Reply With Quote
 
 
 
 
Fancois Grieu
Guest
Posts: n/a
 
      12-10-2007
In article <fjin7q$5s1$(E-Mail Removed)>,
"Sing" <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
 
 
 
Khookie
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 6:21 pm, "Sing" <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-10-2007
Khookie wrote:
> On Dec 10, 6:21 pm, "Sing" <(E-Mail Removed)> wrote:
>> 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.
 
Reply With Quote
 
Toms hilidhe
Guest
Posts: n/a
 
      12-10-2007
"Sing" <(E-Mail Removed)> wrote in
news:fjin7q$5s1$(E-Mail Removed):

> 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
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      12-10-2007
"Tom��������������� ���������������� " wrote:
> "Sing" <(E-Mail Removed)> wrote in
> news:fjin7q$5s1$(E-Mail Removed):
>
>> 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;
 
Reply With Quote
 
Toms hilidhe
Guest
Posts: n/a
 
      12-10-2007
Philip Potter <(E-Mail Removed)> wrote in news:fjk1jd$inh$(E-Mail Removed):

>> 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;
>








--
Toms hilidhe
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 12:21 am, "Sing" <(E-Mail Removed)> wrote:
> 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).
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-10-2007
Khookie wrote:
> "Sing" <(E-Mail Removed)> wrote:
>>
>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Web Stats? Which to use? Which is best? Familyman HTML 3 02-09-2006 11:05 PM
ADSL WIC support - which NM's, and which IOS versions? Kralizec Craig Cisco 5 12-08-2005 02:20 AM
which XMI version compatible to which UML version? Kenny XML 0 06-02-2004 10:20 PM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments