472,794 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 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 1967
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.