473,385 Members | 1,693 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,385 software developers and data experts.

templates and (inline) optimization

leo
I had the believe that the use of templates didn't affect performance.
That is, that from the performance point of view it's the same:

a)
template <class X>
class Y{
X attrib;

...
};

Y<int> myclass;

b)
class Y{
int attrib;
..
};

Y myclass;

However, some weeks ago I discovered that this is not exactly true, at
least for gcc 3.2. Specifically, I had one class of the type b) and I
wanted to converted to the type a) so as that the resulting X class
could have an attrib of any type and not only int. And then,
surprisingly for me, the program turned out to slow down considerably.
I took a look at some basic profiling data and I saw that now some
previously effectively inlined functions where not anymore... (and I
did'nt make any further modification in the code).

I would appreciate any help on this. I would like to know if this is a
limitation of the gcc 3.2 compiler, and then, if this is solved in 4.0
version, or it's a general limitation... and if there is some
workaround more elegant than using macros.

Thank you very much!

Leo.

Jul 23 '05 #1
1 1985
On 10 May 2005 03:39:24 -0700, "leo" <le******@wanadoo.es> wrote:
I had the believe that the use of templates didn't affect performance.
That is, that from the performance point of view it's the same: [...]
However, some weeks ago I discovered that this is not exactly true, at
least for gcc 3.2. Specifically, I had one class of the type b) and I
wanted to converted to the type a) so as that the resulting X class
could have an attrib of any type and not only int. And then,
surprisingly for me, the program turned out to slow down considerably.
I took a look at some basic profiling data and I saw that now some
previously effectively inlined functions where not anymore... (and I
did'nt make any further modification in the code).
Without a complete, concise example it's hard to say for sure what's
happening; do you have such sample code?

Here's a not-too-probable cause: if the function definition is not
visible when the function is called, the function won't be inlined.
As the functions were inlined before you templatized the class, this
probably isn't the cause.

It's also possible that something else causes the slowdown while
turning on profiling disables inlining (try google or gnu.g++.help to
check whether profiling will disable inlining).
I would appreciate any help on this. I would like to know if this is a
limitation of the gcc 3.2 compiler, and then, if this is solved in 4.0
version, or it's a general limitation... and if there is some
workaround more elegant than using macros.

The gang at gnu.g++.help would know more about gcc's limitations.
Offhand I can't think of any restriction in C++ which would prevent a
function from being inlined merely by templatizing it.

As for workarounds, doubtful. In-language, inlining code is achieved
solely through in-class definition or use of the 'inline' keyword and,
in any case, is only a suggestion to the compiler; if the compiler
won't inline template functions, that will be a sticking point.
Pre-language you have macros (as you noted).

Post a complete example and we can do more.

Kanenas
Jul 23 '05 #2

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

Similar topics

3
by: Tim Wesson | last post by:
Does anyone know if it is possible to choose between the below templates (or similar ones) according to the promotion rules for types T and U? Thanks, Tim Wesson.
3
by: Ruben Campos | last post by:
I've found a problem with types defined inside a template. With a non-template class, I can write the following: // MyClass.hpp class MyClass { // ... typedef unsigned int MyType; MyType...
18
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
11
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
6
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
32
by: Immortal Nephi | last post by:
I want to know if the practice is the best. Do I need to place inline keyword inside class definition or outside member function definition. For example class A { public: A(); ~A();
2
by: madhu.srikkanth | last post by:
Hi, I came across a paper by Angelika Langer in C++ Users Journal on Expression Templates. In the article she had mentioned that the code snippet below used to calculate a dot product is an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.