473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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<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
Jul 19 '05 #1
9 3574

"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:6M*******************@news2.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<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


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
Jul 19 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:6M*******************@news2.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<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

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);
Thanks for the tip. This is a good idea.

Sincerely,
Aaron Anodide

john

Jul 19 '05 #3
> >
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 <class T>
inline void ZeroMem(T& obj)

john
Jul 19 '05 #4
"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:6M*******************@news2.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<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


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)

Jul 19 '05 #5

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au...
"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:6M*******************@news2.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<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
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
?
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #6
"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:xi********************@news2.central.cox.net
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.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)

Jul 19 '05 #7

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:xi********************@news2.central.cox.net
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.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


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

Jul 19 '05 #8
"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:cy********************@news2.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.

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)

Jul 19 '05 #9

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"Aaron Anodide" <an*****@hotmail.com> wrote in message
news:cy********************@news2.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

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)

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Dingley | last post by:
I've just started on a new project and inherited a huge pile of XSLT (and I use the term "pile" advisedly !) It runs at glacial speed, and I need to fix this this. Platform is MSXML 4 / ASP ...
3
by: Geng Sheng | last post by:
Hi I am working on a pure managed windows application that creates large byte arrays at run time. In fact it creates a 50M byte array when the application starts public static byte buffer = new...
4
by: Alex Callea | last post by:
Hi there, We have a web application handling thousands of requests per seconds reading sql server data which is heavily updated. We are generally experiencing no performance problems. On some...
1
by: Jon | last post by:
Hello, I have 3 asp.net dll's that are on my host in debug mode, now, the runtime ones won't work, I know the reason why. But i just wanted to know if there#ll be much of a performance...
7
by: Magnus | last post by:
Im using the new binding features of Visual Studio 2005. I have done the steps to create a bound data source, and selected all 40 tables from the database. The wizard generated the necessary code...
5
by: Suhas Vengilat | last post by:
Hi All, In one of the project discussions, one team member raised a question whether the usage of asp.net validation controls (like RequiredFieldValidator, CustomValidator, RegularExpression...
10
by: Hermann.Richter | last post by:
Is there any way to remove automatically all blank chars from the html sent to the client?? Example: ---------------------------------------------------------------- <? echo "HTML tags...
6
by: Rich | last post by:
Hi, I know HOW to run these side by side. But what is the performance impact??? Microsoft make a big deal about how it is supported, but provide no guidelines as to potential performace or...
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've been trying to improve the performance of our xml handling, so I've been doing some timing tests lately and ran across something I thought odd - XPathDocument seems slightly slower...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.