C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

runtime performance impact of template usage

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Aaron Anodide
Guest





PostPosted: Tue Jul 29, 2003 5:39 am    Post subject: runtime performance impact of template usage Reply with quote



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


Back to top
John Harrison
Guest





PostPosted: Tue Jul 29, 2003 6:19 am    Post subject: Re: runtime performance impact of template usage Reply with quote




"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote

Quote:
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
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


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(mystruct);

john



Back to top
Aaron Anodide
Guest





PostPosted: Tue Jul 29, 2003 6:55 am    Post subject: Re: runtime performance impact of template usage Reply with quote




"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:

"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote in message
news:6MnVa.96702$R92.60016 (AT) news2 (DOT) central.cox.net...
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
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


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 void ZeroMem(T& obj)
{
ZeroMemory(&obj, sizeof(T));
}

MYSTRUCT mystruct;
ZeroMem(mystruct);

Thanks for the tip. This is a good idea.

Sincerely,
Aaron Anodide

Quote:

john





Back to top
John Harrison
Guest





PostPosted: Tue Jul 29, 2003 6:57 am    Post subject: Re: runtime performance impact of template usage Reply with quote

Quote:

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(mystruct);

Thanks for the tip. This is a good idea.

Sincerely,
Aaron Anodide


Don't forget to add inline.

template inline void ZeroMem(T& obj)

john



Back to top
John Carson
Guest





PostPosted: Tue Jul 29, 2003 12:24 pm    Post subject: Re: runtime performance impact of template usage Reply with quote

"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote

Quote:
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
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

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)


Back to top
Aaron Anodide
Guest





PostPosted: Tue Jul 29, 2003 3:22 pm    Post subject: Re: runtime performance impact of template usage Reply with quote


"John Carson" <donaldquixote (AT) datafast (DOT) net.au> wrote

Quote:
"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote in message
news:6MnVa.96702$R92.60016 (AT) news2 (DOT) central.cox.net
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
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

I think that it is rather neat the way you derive from a template
parameter


slightly OT, but ATL does this all over the place. I didn't think it up.

Quote:
but, on a more practical level, what's wrong with

MYSTRUCT mystruct = {0};


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

Quote:
?


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




Back to top
John Carson
Guest





PostPosted: Wed Jul 30, 2003 1:53 am    Post subject: Re: runtime performance impact of template usage Reply with quote

"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote

Quote:
"John Carson" <donaldquixote (AT) datafast (DOT) net.au> wrote in message
news:3f266784$1 (AT) usenet (DOT) per.paradox.net.au...

I think that it is rather neat the way you derive from a template
parameter

slightly OT, but ATL does this all over the place. I didn't think it
up.

but, on a more practical level, what's wrong with

MYSTRUCT mystruct = {0};


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


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)


Back to top
Aaron Anodide
Guest





PostPosted: Wed Jul 30, 2003 3:01 am    Post subject: Re: runtime performance impact of template usage Reply with quote


"John Carson" <donaldquixote (AT) datafast (DOT) net.au> wrote

Quote:
"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote in message
news:xiwVa.100493$R92.59493 (AT) news2 (DOT) central.cox.net
"John Carson" <donaldquixote (AT) datafast (DOT) net.au> wrote in message
news:3f266784$1 (AT) usenet (DOT) per.paradox.net.au...

I think that it is rather neat the way you derive from a template
parameter

slightly OT, but ATL does this all over the place. I didn't think it
up.

but, on a more practical level, what's wrong with

MYSTRUCT mystruct = {0};


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


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

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

Quote:


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




Back to top
John Carson
Guest





PostPosted: Wed Jul 30, 2003 3:44 am    Post subject: Re: runtime performance impact of template usage Reply with quote

"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote

Quote:

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


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.


Quote:
What about nested structs?

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

will your initialization scheme work here too?

Aaron



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)


Back to top
Aaron Anodide
Guest





PostPosted: Wed Jul 30, 2003 6:45 am    Post subject: Re: runtime performance impact of template usage Reply with quote


"John Carson" <donaldquixote (AT) datafast (DOT) net.au> wrote

Quote:
"Aaron Anodide" <anodide (AT) hotmail (DOT) com> wrote in message
news:cyGVa.105163$R92.76256 (AT) news2 (DOT) central.cox.net

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


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.

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

Quote:


What about nested structs?

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

will your initialization scheme work here too?

Aaron



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)




Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.