Which max() is faster?

Discussion in 'C Programming' started by Sing, Dec 10, 2007.

  1. Sing

    Sing Guest

    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?
     
    Sing, Dec 10, 2007
    #1
    1. Advertising

  2. In article <fjin7q$5s1$>,
    "Sing" <> 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
     
    Fancois Grieu, Dec 10, 2007
    #2
    1. Advertising

  3. Sing

    Khookie Guest

    On Dec 10, 6:21 pm, "Sing" <> 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.
     
    Khookie, Dec 10, 2007
    #3
  4. Sing

    James Kuyper Guest

    Khookie wrote:
    > On Dec 10, 6:21 pm, "Sing" <> 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.
     
    James Kuyper, Dec 10, 2007
    #4
  5. "Sing" <> wrote in
    news:fjin7q$5s1$:

    > 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_(computer_science)

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Dec 10, 2007
    #5
  6. "Tom��������������������������������" wrote:
    > "Sing" <> wrote in
    > news:fjin7q$5s1$:
    >
    >> 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;
     
    Philip Potter, Dec 10, 2007
    #6
  7. Philip Potter <> wrote in news:fjk1jd$inh$:

    >> 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
     
    Tomás Ó hÉilidhe, Dec 10, 2007
    #7
  8. Sing

    user923005 Guest

    On Dec 10, 12:21 am, "Sing" <> 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).
     
    user923005, Dec 10, 2007
    #8
  9. Sing

    CBFalconer Guest

    Khookie wrote:
    > "Sing" <> 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
     
    CBFalconer, Dec 10, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,710
  2. Summercool
    Replies:
    9
    Views:
    925
    dorayme
    Oct 23, 2007
  3. Eirik
    Replies:
    10
    Views:
    536
    Jorgen Grahn
    Feb 23, 2012
  4. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    627
    Greg Ferris
    Jan 16, 2004
  5. kriton
    Replies:
    0
    Views:
    368
    kriton
    Apr 22, 2005
Loading...

Share This Page