Click here to Skip to main content
15,917,565 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: pointer var Pin
Alexander M.,8-Sep-03 0:29
Alexander M.,8-Sep-03 0:29 
GeneralRe: pointer var Pin
macmac388-Sep-03 0:41
macmac388-Sep-03 0:41 
GeneralRe: pointer var Pin
jhwurmbach8-Sep-03 1:23
jhwurmbach8-Sep-03 1:23 
GeneralGUI database query Pin
coda_x8-Sep-03 0:08
coda_x8-Sep-03 0:08 
GeneralRe: GUI database query Pin
David Crow8-Sep-03 3:35
David Crow8-Sep-03 3:35 
GeneralCHttpView - Viewing and modifing post header Pin
Brian Delahunty8-Sep-03 0:07
Brian Delahunty8-Sep-03 0:07 
GeneralSpeed of Templates vs. Non-Templates Pin
RichardS7-Sep-03 23:44
RichardS7-Sep-03 23:44 
GeneralRe: Speed of Templates vs. Non-Templates Pin
Andrew Walker8-Sep-03 0:24
Andrew Walker8-Sep-03 0:24 
RichardS wrote:
1. Does anyone know if using templates causes classes to run slower in time versus there non-templated counter parts?

The overhead for templates is at compile time. It manifests itself in two ways:

1/ slower build times
2/ code bloat

Slower Build Times for the most aren't a real problem, but with some libraries there may be a noticable slowdown. I noticed this mostly when dealing with boost::function. However, the benefits you get from templates far outweigh this in my opinion.

Code Bloat is caused by repeated instantiation of different types of templates making your compiled products bigger.

As for whether templates can make code faster at runtime, they can, but only if you are prepared to make sacrifices in other areas. For example, sometimes you can avoid the use of virtual functions altogether by using templates, which in tight inner loops may give you the speed increase you need.

Another area templates can make a significant performance difference is in compile time calculations - many matrix math libraries now use templated proxy classes to minimise copies.

Templates certainly shouldn't be any slower at runtime than normal classes, because they will eventually be converted to standard classes ( well, sort of Wink | ;) ). It's certainly a matter of picking the right tools for the job.

RichardS wrote:
2. As I understand that templates allow for portability, is it wise to use them for time critical operations (eg. graphics processing, DSP, etc.)?

Again, performance depends on the situation, if you can prevent extra copies (look for information on Expression templates, or look at the code for boost::lambda or boost::ublas for good examples), you are likely to get significant speed improvements. If you need to matrix math (DSP) you may well get that extra performance you need.

Portability (as in operating system portability) is more generally supported by the use of the preprocessor, but it should be possible to use template specialisations to do the same thing - but I don't know if it would be worth the effort.

RichardS wrote:
3. Are the STL classes fast?

The STL classes were designed to be generic containers, so they aren't optimal in all situations. For most applications the effort and risk of writing and debugging custom containers is probably higher than that of using the STL. If you're sensible with the containers you can get extremely good performance out of them, eg/ store pointers (or even better, boost::shared_ptr's) to minimise the cost of copying containers. Also make sure you select the appropriate container for the job.

The STL learning curve isn't as steep as many other API's, and I strongly recommend this as a good way to get into using writing generic code. What you will find is that writing good generic code is harder than writing non-generic code, but it's (generally Smile | :) )almost as easy to use.


If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts you aim;
Yours is the Earth and everything that's in it.

Rudyard Kipling

GeneralRe: Speed of Templates vs. Non-Templates Pin
RichardS8-Sep-03 1:37
RichardS8-Sep-03 1:37 
GeneralRe: Speed of Templates vs. Non-Templates Pin
antlers9-Sep-03 9:40
antlers9-Sep-03 9:40 
QuestionCan not find class Pin
wow99997-Sep-03 21:49
wow99997-Sep-03 21:49 
AnswerRe: Can not find class Pin
jhwurmbach8-Sep-03 1:27
jhwurmbach8-Sep-03 1:27 
AnswerRe: Can not find class Pin
David Crow8-Sep-03 3:39
David Crow8-Sep-03 3:39 
AnswerRe: Can not find class Pin
imsniper8-Sep-03 3:50
imsniper8-Sep-03 3:50 
QuestionSQL Connect error???? Pin
xxhimanshu7-Sep-03 20:05
xxhimanshu7-Sep-03 20:05 
AnswerRe: SQL Connect error???? Pin
David Crow8-Sep-03 3:42
David Crow8-Sep-03 3:42 
GeneralRe: SQL Connect error???? Pin
xxhimanshu8-Sep-03 17:58
xxhimanshu8-Sep-03 17:58 
GeneralRe: SQL Connect error???? Pin
David Crow9-Sep-03 2:12
David Crow9-Sep-03 2:12 
GeneralRe: SQL Connect error???? Pin
xxhimanshu10-Sep-03 22:14
xxhimanshu10-Sep-03 22:14 
GeneralProblem with code Pin
Ricky Koh7-Sep-03 19:19
Ricky Koh7-Sep-03 19:19 
GeneralRe: Problem with code Pin
Michael Dunn7-Sep-03 20:01
sitebuilderMichael Dunn7-Sep-03 20:01 
GeneralRe: Problem with code Pin
Ricky Koh7-Sep-03 20:22
Ricky Koh7-Sep-03 20:22 
GeneralRe: Problem with code Pin
David Crow8-Sep-03 3:44
David Crow8-Sep-03 3:44 
GeneralRe: Problem with code Pin
Ricky Koh8-Sep-03 5:05
Ricky Koh8-Sep-03 5:05 
GeneralRe: Problem with code Pin
David Crow8-Sep-03 5:40
David Crow8-Sep-03 5:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.