macro vs. template???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aaragon

    macro vs. template???

    Hi everyone. A very simple question. I would like to know what is
    better in terms of performance. I want to use a simple function to
    obtain the minimum of two values. One way could be using a macro:

    #define min(a,b) ((a<b)?a:b)

    I thought that another way could be to use a template function:

    template <class T>
    T min<T a, T b>
    {
    if(a < b)
    return a;
    else
    return b;
    }

    Any ideas on which way is better to implement? I guess that whenever I
    use the template function is going to help me check at compile time.
    Also, for some reason I don't like using macros. Thank you.

  • Ian Collins

    #2
    Re: macro vs. template???

    aaragon wrote:
    Hi everyone. A very simple question. I would like to know what is
    better in terms of performance. I want to use a simple function to
    obtain the minimum of two values. One way could be using a macro:
    >
    #define min(a,b) ((a<b)?a:b)
    >
    I thought that another way could be to use a template function:
    >
    template <class T>
    T min<T a, T b>
    {
    if(a < b)
    return a;
    else
    return b;
    }
    >
    Any ideas on which way is better to implement? I guess that whenever I
    use the template function is going to help me check at compile time.
    Also, for some reason I don't like using macros. Thank you.
    >
    What did you profiler show you?

    Follow your instinct and don't use macros.

    --
    Ian Collins.

    Comment

    • Noah Roberts

      #3
      Re: macro vs. template???


      aaragon wrote:
      Hi everyone. A very simple question. I would like to know what is
      better in terms of performance. I want to use a simple function to
      obtain the minimum of two values. One way could be using a macro:
      >
      #define min(a,b) ((a<b)?a:b)
      >
      I thought that another way could be to use a template function:
      >
      template <class T>
      T min<T a, T b>
      {
      if(a < b)
      return a;
      else
      return b;
      }
      >
      Version two has better type safety. Not much can go wrong with that
      little function but regardless, practice is better in the second. You
      might consider references though.
      Any ideas on which way is better to implement? I guess that whenever I
      use the template function is going to help me check at compile time.
      Also, for some reason I don't like using macros. Thank you.
      There is no reason to not like using macros. Use them when
      appropriate. Macros just aren't appropriate for inline stuff in
      C++...you should use inline functions (don't have to be templates).

      Comment

      • Kai-Uwe Bux

        #4
        Re: macro vs. template???

        aaragon wrote:
        Hi everyone. A very simple question. I would like to know what is
        better in terms of performance. I want to use a simple function to
        obtain the minimum of two values. One way could be using a macro:
        >
        #define min(a,b) ((a<b)?a:b)
        >
        I thought that another way could be to use a template function:
        >
        template <class T>
        T min<T a, T b>
        {
        if(a < b)
        return a;
        else
        return b;
        }
        >
        Any ideas on which way is better to implement? I guess that whenever I
        use the template function is going to help me check at compile time.
        Also, for some reason I don't like using macros. Thank you.
        Why not use std::min() from <algorithm?


        Best

        Kai-Uwe Bux

        Comment

        • Phlip

          #5
          Re: macro vs. template???

          Noah Roberts wrote:
          There is no reason to not like using macros. Use them when
          appropriate. Macros just aren't appropriate for inline stuff in
          C++...you should use inline functions (don't have to be templates).
          Further, inline doesn't automatically mean faster.

          In general, programmers rarely need to worry about performance at the level
          of individual statements and functions. Write your code, make it clean, and
          upgrade its algorithm, and you should be fine.

          Lots of C++ tutorials talk about optimization as if that's what we do all
          day. Optimization comes down to the 'if' statements only very rarely.
          Premature optimization is the root of all evil; you should think of clear
          and expressive statements, first. (That means min should be a template
          function, because that's easiest to get right without worrying about it.)

          --
          Phlip
          http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


          Comment

          • Mark P

            #6
            Re: macro vs. template???

            aaragon wrote:
            Hi everyone. A very simple question. I would like to know what is
            better in terms of performance. I want to use a simple function to
            obtain the minimum of two values. One way could be using a macro:
            >
            #define min(a,b) ((a<b)?a:b)
            >
            I thought that another way could be to use a template function:
            >
            template <class T>
            T min<T a, T b>
            {
            if(a < b)
            return a;
            else
            return b;
            }
            >
            Any ideas on which way is better to implement? I guess that whenever I
            use the template function is going to help me check at compile time.
            Also, for some reason I don't like using macros. Thank you.
            >
            Unlikely you'll notice a performance difference but note that you've
            chosen a notoriously bad macro as your example. If expression a or b
            has any side effects then these two are not equivalent. Additionally,
            that you haven't enclosed all occurrences of a and b in parentheses in
            the macro definition leaves open the possibility of subtle errors due to
            operator precedence.

            Comment

            • aaragon

              #7
              Re: macro vs. template???


              Mark P wrote:
              aaragon wrote:
              Hi everyone. A very simple question. I would like to know what is
              better in terms of performance. I want to use a simple function to
              obtain the minimum of two values. One way could be using a macro:

              #define min(a,b) ((a<b)?a:b)

              I thought that another way could be to use a template function:

              template <class T>
              T min<T a, T b>
              {
              if(a < b)
              return a;
              else
              return b;
              }

              Any ideas on which way is better to implement? I guess that whenever I
              use the template function is going to help me check at compile time.
              Also, for some reason I don't like using macros. Thank you.
              >
              Unlikely you'll notice a performance difference but note that you've
              chosen a notoriously bad macro as your example. If expression a or b
              has any side effects then these two are not equivalent. Additionally,
              that you haven't enclosed all occurrences of a and b in parentheses in
              the macro definition leaves open the possibility of subtle errors due to
              operator precedence.
              Thanks everybody for your answers. I will stick to the template for
              safety reasons. I never worked with inline functions so I don't know
              what benefits they have.

              Comment

              • Ian Collins

                #8
                Re: macro vs. template???

                aaragon wrote:
                >
                Thanks everybody for your answers. I will stick to the template for
                safety reasons. I never worked with inline functions so I don't know
                what benefits they have.
                >
                Have you ever written trivial method bodies in a class declaration? If
                so, you have worked with inline functions.

                You compiler will inline small methods when you compile with
                optimisation, so don't get too hung up on what is and what isn't an
                inline function.

                --
                Ian Collins.

                Comment

                • Alan Johnson

                  #9
                  Re: macro vs. template???


                  aaragon wrote:
                  Hi everyone. A very simple question. I would like to know what is
                  better in terms of performance. I want to use a simple function to
                  obtain the minimum of two values. One way could be using a macro:
                  >
                  #define min(a,b) ((a<b)?a:b)
                  >
                  I thought that another way could be to use a template function:
                  >
                  template <class T>
                  T min<T a, T b>
                  {
                  if(a < b)
                  return a;
                  else
                  return b;
                  }
                  >
                  Any ideas on which way is better to implement? I guess that whenever I
                  use the template function is going to help me check at compile time.
                  Also, for some reason I don't like using macros. Thank you.
                  There is a lot more depth to this question than what most people would
                  ever really imagine. See the following article by Andrei Alexandrescu:



                  --
                  Alan Johnson

                  Comment

                  • aaragon

                    #10
                    Re: macro vs. template???


                    Alan Johnson wrote:
                    aaragon wrote:
                    Hi everyone. A very simple question. I would like to know what is
                    better in terms of performance. I want to use a simple function to
                    obtain the minimum of two values. One way could be using a macro:

                    #define min(a,b) ((a<b)?a:b)

                    I thought that another way could be to use a template function:

                    template <class T>
                    T min<T a, T b>
                    {
                    if(a < b)
                    return a;
                    else
                    return b;
                    }

                    Any ideas on which way is better to implement? I guess that whenever I
                    use the template function is going to help me check at compile time.
                    Also, for some reason I don't like using macros. Thank you.
                    >
                    There is a lot more depth to this question than what most people would
                    ever really imagine. See the following article by Andrei Alexandrescu:
                    >

                    >
                    --
                    Alan Johnson
                    I never thought that a min function using templates was so complicated.
                    Untl I understand what Alexandrescu is doing, I finally decided to
                    stick to the macro version.

                    Thanks Alan for pointing out that article.

                    Comment

                    • aaragon

                      #11
                      Re: macro vs. template???


                      Ian Collins wrote:
                      aaragon wrote:

                      Thanks everybody for your answers. I will stick to the template for
                      safety reasons. I never worked with inline functions so I don't know
                      what benefits they have.
                      Have you ever written trivial method bodies in a class declaration? If
                      so, you have worked with inline functions.
                      >
                      You compiler will inline small methods when you compile with
                      optimisation, so don't get too hung up on what is and what isn't an
                      inline function.
                      >
                      --
                      Ian Collins.
                      Ian, I see what you mean with that. Do I put the "inline" word in
                      front of the trivial method so that it can be inlined by the compiler?
                      Or the compiler knows that the method can be inlined by its own?

                      Comment

                      • Ian Collins

                        #12
                        Re: macro vs. template???

                        aaragon wrote:
                        Ian Collins wrote:
                        >
                        >>aaragon wrote:
                        >>
                        >>>Thanks everybody for your answers. I will stick to the template for
                        >>>safety reasons. I never worked with inline functions so I don't know
                        >>>what benefits they have.
                        >>>
                        >>
                        >>Have you ever written trivial method bodies in a class declaration? If
                        >>so, you have worked with inline functions.
                        >>
                        >>You compiler will inline small methods when you compile with
                        >>optimisatio n, so don't get too hung up on what is and what isn't an
                        >>inline function.
                        >>
                        >>--
                        >>Ian Collins.
                        >
                        >
                        Ian, I see what you mean with that. Do I put the "inline" word in
                        front of the trivial method so that it can be inlined by the compiler?
                        Or the compiler knows that the method can be inlined by its own?
                        >
                        'inline' is nothing more than a hint.

                        Modern compilers (especially if they have profiling feedback) do a
                        decent job on their own. That's one of the beauties of C++, you can
                        write short functions with descriptive names to improve the readability
                        of your code knowing the compiler will inline them.

                        --
                        Ian Collins.

                        Comment

                        • Ian Collins

                          #13
                          Re: macro vs. template???

                          aaragon wrote:
                          Alan Johnson wrote:
                          >
                          >>aaragon wrote:
                          >>
                          >>>Hi everyone. A very simple question. I would like to know what is
                          >>>better in terms of performance. I want to use a simple function to
                          >>>obtain the minimum of two values. One way could be using a macro:
                          >>>
                          >>>#define min(a,b) ((a<b)?a:b)
                          >>>
                          >>>I thought that another way could be to use a template function:
                          >>>
                          >>>template <class T>
                          >>>T min<T a, T b>
                          >>>{
                          >> if(a < b)
                          >> return a;
                          >> else
                          >> return b;
                          >>>}
                          >>>
                          >>>Any ideas on which way is better to implement? I guess that whenever I
                          >>>use the template function is going to help me check at compile time.
                          >>>Also, for some reason I don't like using macros. Thank you.
                          >>
                          >>There is a lot more depth to this question than what most people would
                          >>ever really imagine. See the following article by Andrei Alexandrescu:
                          >>
                          >>http://www.ddj.com/dept/cpp/184403774
                          >>
                          >
                          I never thought that a min function using templates was so complicated.
                          Untl I understand what Alexandrescu is doing, I finally decided to
                          stick to the macro version.
                          >
                          Better to do as Kai-Uwe Bux suggested and use std::min().

                          --
                          Ian Collins.

                          Comment

                          • Risto Lankinen

                            #14
                            Re: macro vs. template???


                            "aaragon" <alejandro.arag on@gmail.comwro te in message
                            news:1161835488 .390546.144310@ m73g2000cwd.goo glegroups.com.. .
                            I never thought that a min function using templates was so complicated.
                            Untl I understand what Alexandrescu is doing, I finally decided to
                            stick to the macro version.
                            >
                            Thanks Alan for pointing out that article.
                            Don't fret, the article overshoots in one crucial way: Who will
                            ever need a min() or max() function that returns either a const
                            or a non-const reference based on the *run-time* values of
                            its arguments?

                            Put differently, the article aims to make something like this to
                            compileable:

                            int r = rand();
                            const int s = rand();
                            max( r,s ) = rand();

                            The real question is, why should it compile?!? If there's mixed
                            constness in the arguments of max() or min(), and the run-time
                            values of said arguments determine the result, then how can
                            the programmer expect the assignment to succeed if the result
                            turns out to be the const one?

                            (BTW, It's entirely possible that I'm missing some significant
                            point in my criticism, in which case please educate me.)

                            - Risto -


                            Comment

                            • Ron Natalie

                              #15
                              Re: macro vs. template???

                              aaragon wrote:
                              Hi everyone. A very simple question. I would like to know what is
                              better in terms of performance. I want to use a simple function to
                              obtain the minimum of two values. One way could be using a macro:
                              >
                              #define min(a,b) ((a<b)?a:b)
                              >
                              I thought that another way could be to use a template function:
                              >
                              template <class T>
                              T min<T a, T b>
                              {
                              if(a < b)
                              return a;
                              else
                              return b;
                              }
                              >
                              Any ideas on which way is better to implement? I guess that whenever I
                              use the template function is going to help me check at compile time.
                              Also, for some reason I don't like using macros. Thank you.
                              >
                              Well they aren't semantically equivalent. The first one evaluates
                              the expression that is the smallest value twice.

                              Comment

                              Working...