runtime performance impact of template usage

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

    runtime performance impact of template usage

    Hello,

    I am using the following template class as a shorthand for zero-ing memory:

    template<class T>
    class ZeroMem : public T
    {
    public:

    ZeroMem(void)
    {
    ZeroMemory( this, sizeof(T) );
    }
    };

    Then, I do things like:

    ZeroMem<MYSTRUC T> mystruct;

    My question is: Does using this have any runtime performance impact? My
    hope is that the inline constructor causes this usage of this template to be
    eqiv to a macro.

    Thanks,
    Aaron Anodide


  • John Harrison

    #2
    Re: runtime performance impact of template usage


    "Aaron Anodide" <anodide@hotmai l.com> wrote in message
    news:6MnVa.9670 2$R92.60016@new s2.central.cox. net...[color=blue]
    > Hello,
    >
    > I am using the following template class as a shorthand for zero-ing[/color]
    memory:[color=blue]
    >
    > template<class T>
    > class ZeroMem : public T
    > {
    > public:
    >
    > ZeroMem(void)
    > {
    > ZeroMemory( this, sizeof(T) );
    > }
    > };
    >
    > Then, I do things like:
    >
    > ZeroMem<MYSTRUC T> mystruct;
    >
    > My question is: Does using this have any runtime performance impact? My
    > hope is that the inline constructor causes this usage of this template to[/color]
    be[color=blue]
    > eqiv to a macro.
    >
    > Thanks,
    > Aaron Anodide
    >[/color]

    The *only* way to find out would be to look at the machine code generated by
    your compiler. C++ does not require inline funcitons to be actually inlined,
    its only a hint.

    In any case the overhead for a function call is nano-seconds, are you
    claiming that this would make a noticeable difference to your program? Have
    you timed anything? Unless the answer to both those questions is yes, you
    should be concentrating on writing clear code, not tricks with templates.

    I think your method is somewhat dubious because you are creating different
    types from what you really want. Isn't a template function better

    template <class T>
    void ZeroMem(T& obj)
    {
    ZeroMemory(&obj , sizeof(T));
    }

    MYSTRUCT mystruct;
    ZeroMem(mystruc t);

    john


    Comment

    • Aaron Anodide

      #3
      Re: runtime performance impact of template usage


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:bg53kq$kop 6b$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "Aaron Anodide" <anodide@hotmai l.com> wrote in message
      > news:6MnVa.9670 2$R92.60016@new s2.central.cox. net...[color=green]
      > > Hello,
      > >
      > > I am using the following template class as a shorthand for zero-ing[/color]
      > memory:[color=green]
      > >
      > > template<class T>
      > > class ZeroMem : public T
      > > {
      > > public:
      > >
      > > ZeroMem(void)
      > > {
      > > ZeroMemory( this, sizeof(T) );
      > > }
      > > };
      > >
      > > Then, I do things like:
      > >
      > > ZeroMem<MYSTRUC T> mystruct;
      > >
      > > My question is: Does using this have any runtime performance impact? My
      > > hope is that the inline constructor causes this usage of this template[/color][/color]
      to[color=blue]
      > be[color=green]
      > > eqiv to a macro.
      > >
      > > Thanks,
      > > Aaron Anodide
      > >[/color]
      >
      > The *only* way to find out would be to look at the machine code generated[/color]
      by[color=blue]
      > your compiler. C++ does not require inline funcitons to be actually[/color]
      inlined,[color=blue]
      > its only a hint.
      >
      > In any case the overhead for a function call is nano-seconds, are you
      > claiming that this would make a noticeable difference to your program?[/color]
      Have[color=blue]
      > you timed anything? Unless the answer to both those questions is yes, you
      > should be concentrating on writing clear code, not tricks with templates.
      >
      > I think your method is somewhat dubious because you are creating different
      > types from what you really want. Isn't a template function better
      >
      > template <class T>
      > void ZeroMem(T& obj)
      > {
      > ZeroMemory(&obj , sizeof(T));
      > }
      >
      > MYSTRUCT mystruct;
      > ZeroMem(mystruc t);[/color]

      Thanks for the tip. This is a good idea.

      Sincerely,
      Aaron Anodide
      [color=blue]
      >
      > john
      >
      >[/color]


      Comment

      • John Harrison

        #4
        Re: runtime performance impact of template usage

        > >[color=blue][color=green]
        > > I think your method is somewhat dubious because you are creating[/color][/color]
        different[color=blue][color=green]
        > > types from what you really want. Isn't a template function better
        > >
        > > template <class T>
        > > void ZeroMem(T& obj)
        > > {
        > > ZeroMemory(&obj , sizeof(T));
        > > }
        > >
        > > MYSTRUCT mystruct;
        > > ZeroMem(mystruc t);[/color]
        >
        > Thanks for the tip. This is a good idea.
        >
        > Sincerely,
        > Aaron Anodide
        >[/color]

        Don't forget to add inline.

        template <class T>
        inline void ZeroMem(T& obj)

        john


        Comment

        • John Carson

          #5
          Re: runtime performance impact of template usage

          "Aaron Anodide" <anodide@hotmai l.com> wrote in message
          news:6MnVa.9670 2$R92.60016@new s2.central.cox. net[color=blue]
          > Hello,
          >
          > I am using the following template class as a shorthand for zero-ing
          > memory:
          >
          > template<class T>
          > class ZeroMem : public T
          > {
          > public:
          >
          > ZeroMem(void)
          > {
          > ZeroMemory( this, sizeof(T) );
          > }
          > };
          >
          > Then, I do things like:
          >
          > ZeroMem<MYSTRUC T> mystruct;
          >
          > My question is: Does using this have any runtime performance impact?
          > My hope is that the inline constructor causes this usage of this
          > template to be eqiv to a macro.
          >
          > Thanks,
          > Aaron Anodide[/color]

          I think that it is rather neat the way you derive from a template parameter
          but, on a more practical level, what's wrong with

          MYSTRUCT mystruct = {0};

          ?


          --
          John Carson
          1. To reply to email address, remove donald
          2. Don't reply to email address (post here instead)

          Comment

          • Aaron Anodide

            #6
            Re: runtime performance impact of template usage


            "John Carson" <donaldquixote@ datafast.net.au > wrote in message
            news:3f266784$1 @usenet.per.par adox.net.au...[color=blue]
            > "Aaron Anodide" <anodide@hotmai l.com> wrote in message
            > news:6MnVa.9670 2$R92.60016@new s2.central.cox. net[color=green]
            > > Hello,
            > >
            > > I am using the following template class as a shorthand for zero-ing
            > > memory:
            > >
            > > template<class T>
            > > class ZeroMem : public T
            > > {
            > > public:
            > >
            > > ZeroMem(void)
            > > {
            > > ZeroMemory( this, sizeof(T) );
            > > }
            > > };
            > >
            > > Then, I do things like:
            > >
            > > ZeroMem<MYSTRUC T> mystruct;
            > >
            > > My question is: Does using this have any runtime performance impact?
            > > My hope is that the inline constructor causes this usage of this
            > > template to be eqiv to a macro.
            > >
            > > Thanks,
            > > Aaron Anodide[/color]
            >
            > I think that it is rather neat the way you derive from a template[/color]
            parameter

            slightly OT, but ATL does this all over the place. I didn't think it up.
            [color=blue]
            > but, on a more practical level, what's wrong with
            >
            > MYSTRUCT mystruct = {0};
            >[/color]

            Honestly, I wasn't aware of that. I've seen reams of example code using
            ZeroMemory on windows or memset(). I've never seen ={0}; Is this standard
            c++?

            Thanks,
            Aaron
            [color=blue]
            > ?
            >
            >
            > --
            > John Carson
            > 1. To reply to email address, remove donald
            > 2. Don't reply to email address (post here instead)
            >[/color]


            Comment

            • John Carson

              #7
              Re: runtime performance impact of template usage

              "Aaron Anodide" <anodide@hotmai l.com> wrote in message
              news:xiwVa.1004 93$R92.59493@ne ws2.central.cox .net[color=blue]
              > "John Carson" <donaldquixote@ datafast.net.au > wrote in message
              > news:3f266784$1 @usenet.per.par adox.net.au...[color=green]
              > >
              > > I think that it is rather neat the way you derive from a template
              > > parameter[/color]
              >
              > slightly OT, but ATL does this all over the place. I didn't think it
              > up.
              >[color=green]
              > > but, on a more practical level, what's wrong with
              > >
              > > MYSTRUCT mystruct = {0};
              > >[/color]
              >
              > Honestly, I wasn't aware of that. I've seen reams of example code
              > using ZeroMemory on windows or memset(). I've never seen ={0}; Is
              > this standard c++?
              >
              > Thanks,
              > Aaron[/color]


              Yes, it is standard. It can't be used for dynamically allocated memory, but
              it works for all other memory allocation. It is just a special case of the
              rules for struct initialisation. Given a plain struct (no constructor etc.)
              like

              struct S
              {
              int x,
              char *str,
              int y;
              };

              You can initialise it with, say,

              S s = {5, "Name", 9};

              The rules say that if you only partially initialise the struct, e.g.,

              S s = {5};

              then everything in the remainder of the struct is initialised to zero, i.e.,
              the preceding line is equivalent to

              S s = {5, 0, 0};

              Thus if you enter

              S s = {0};

              then this is equivalent to

              S s = {0, 0, 0};


              --
              John Carson
              1. To reply to email address, remove donald
              2. Don't reply to email address (post here instead)

              Comment

              • Aaron Anodide

                #8
                Re: runtime performance impact of template usage


                "John Carson" <donaldquixote@ datafast.net.au > wrote in message
                news:3f272536@u senet.per.parad ox.net.au...[color=blue]
                > "Aaron Anodide" <anodide@hotmai l.com> wrote in message
                > news:xiwVa.1004 93$R92.59493@ne ws2.central.cox .net[color=green]
                > > "John Carson" <donaldquixote@ datafast.net.au > wrote in message
                > > news:3f266784$1 @usenet.per.par adox.net.au...[color=darkred]
                > > >
                > > > I think that it is rather neat the way you derive from a template
                > > > parameter[/color]
                > >
                > > slightly OT, but ATL does this all over the place. I didn't think it
                > > up.
                > >[color=darkred]
                > > > but, on a more practical level, what's wrong with
                > > >
                > > > MYSTRUCT mystruct = {0};
                > > >[/color]
                > >
                > > Honestly, I wasn't aware of that. I've seen reams of example code
                > > using ZeroMemory on windows or memset(). I've never seen ={0}; Is
                > > this standard c++?
                > >
                > > Thanks,
                > > Aaron[/color]
                >
                >
                > Yes, it is standard. It can't be used for dynamically allocated memory,[/color]
                but[color=blue]
                > it works for all other memory allocation. It is just a special case of the
                > rules for struct initialisation. Given a plain struct (no constructor[/color]
                etc.)[color=blue]
                > like
                >
                > struct S
                > {
                > int x,
                > char *str,
                > int y;
                > };
                >
                > You can initialise it with, say,
                >
                > S s = {5, "Name", 9};
                >
                > The rules say that if you only partially initialise the struct, e.g.,
                >
                > S s = {5};
                >
                > then everything in the remainder of the struct is initialised to zero,[/color]
                i.e.,[color=blue]
                > the preceding line is equivalent to
                >
                > S s = {5, 0, 0};
                >
                > Thus if you enter
                >
                > S s = {0};
                >
                > then this is equivalent to
                >
                > S s = {0, 0, 0};[/color]

                Thanks for the info. I wonder why so many people use memset or ZeroMemory
                on stack variable structs then?

                What about nested structs?

                struct S
                {
                int x;
                };
                struct T
                {
                int y;
                S s;
                };

                will your initialization scheme work here too?

                Aaron
                [color=blue]
                >
                >
                > --
                > John Carson
                > 1. To reply to email address, remove donald
                > 2. Don't reply to email address (post here instead)
                >[/color]


                Comment

                • John Carson

                  #9
                  Re: runtime performance impact of template usage

                  "Aaron Anodide" <anodide@hotmai l.com> wrote in message
                  news:cyGVa.1051 63$R92.76256@ne ws2.central.cox .net[color=blue]
                  >
                  > Thanks for the info. I wonder why so many people use memset or
                  > ZeroMemory on stack variable structs then?[/color]


                  The ={0} approach is a technique inherited from C. People brought up on
                  constructors may not be familiar with it or may not have bothered to
                  remember it. Stroustrup discusses it in TC++PL.

                  [color=blue]
                  > What about nested structs?
                  >
                  > struct S
                  > {
                  > int x;
                  > };
                  > struct T
                  > {
                  > int y;
                  > S s;
                  > };
                  >
                  > will your initialization scheme work here too?
                  >
                  > Aaron
                  >[/color]


                  Yes. Just to make it slightly more interesting, suppose we have:

                  struct S
                  {
                  int w, x;
                  };

                  struct T
                  {
                  int y;
                  S s;
                  };

                  You could explicitly initialise this with:

                  T t = {3, {5, 7}};

                  The nested brackets, however, only make the code visually clearer. The
                  following is equivalent:

                  T t = {3, 5, 7};

                  The same rules apply. If you only partially initialise, then everything not
                  explicitly initialised is set to zero. Thus

                  T t = {3};

                  sets the nested S structure to zero, while

                  T t = {0};

                  sets everything to zero.


                  --
                  John Carson
                  1. To reply to email address, remove donald
                  2. Don't reply to email address (post here instead)

                  Comment

                  • Aaron Anodide

                    #10
                    Re: runtime performance impact of template usage


                    "John Carson" <donaldquixote@ datafast.net.au > wrote in message
                    news:3f273f50@u senet.per.parad ox.net.au...[color=blue]
                    > "Aaron Anodide" <anodide@hotmai l.com> wrote in message
                    > news:cyGVa.1051 63$R92.76256@ne ws2.central.cox .net[color=green]
                    > >
                    > > Thanks for the info. I wonder why so many people use memset or
                    > > ZeroMemory on stack variable structs then?[/color]
                    >
                    >
                    > The ={0} approach is a technique inherited from C. People brought up on
                    > constructors may not be familiar with it or may not have bothered to
                    > remember it. Stroustrup discusses it in TC++PL.[/color]

                    Thanks again for the background info. It's very interesting to learn
                    something I did not know previously.

                    As a footnote, I wonder if the guys writing the Unit test cases for my C++
                    compiler forgot about the ={0} as well.....

                    Aaron
                    [color=blue]
                    >
                    >[color=green]
                    > > What about nested structs?
                    > >
                    > > struct S
                    > > {
                    > > int x;
                    > > };
                    > > struct T
                    > > {
                    > > int y;
                    > > S s;
                    > > };
                    > >
                    > > will your initialization scheme work here too?
                    > >
                    > > Aaron
                    > >[/color]
                    >
                    >
                    > Yes. Just to make it slightly more interesting, suppose we have:
                    >
                    > struct S
                    > {
                    > int w, x;
                    > };
                    >
                    > struct T
                    > {
                    > int y;
                    > S s;
                    > };
                    >
                    > You could explicitly initialise this with:
                    >
                    > T t = {3, {5, 7}};
                    >
                    > The nested brackets, however, only make the code visually clearer. The
                    > following is equivalent:
                    >
                    > T t = {3, 5, 7};
                    >
                    > The same rules apply. If you only partially initialise, then everything[/color]
                    not[color=blue]
                    > explicitly initialised is set to zero. Thus
                    >
                    > T t = {3};
                    >
                    > sets the nested S structure to zero, while
                    >
                    > T t = {0};
                    >
                    > sets everything to zero.
                    >
                    >
                    > --
                    > John Carson
                    > 1. To reply to email address, remove donald
                    > 2. Don't reply to email address (post here instead)
                    >[/color]


                    Comment

                    Working...