Class Templates

A

Andre

Hi,

I've built a math library that does some matrix multiplications and
other linear algebric functions. I read some place about generics or
class templates and I was wondering if this will help me in any way? I
have structures and classes with the same logic but that which deal with
different types. Moreover, I would like to know if there are any
performance benifits or drawbacks and how templates are or can be
important to numerical code (typically high-performance code). Thanks,

regards

-Andre
 
D

Daniel Seitz

Andre wrote in message said:
Hi,

I've built a math library that does some matrix multiplications and
other linear algebric functions. I read some place about generics or
class templates and I was wondering if this will help me in any way? I
have structures and classes with the same logic but that which deal with
different types. Moreover, I would like to know if there are any
performance benifits or drawbacks and how templates are or can be
important to numerical code (typically high-performance code). Thanks,

regards

-Andre

Well, first of all you can support other types (float, double, or a class
type that can hold more accurate numbers). Second, you can take advantage of
the nature of templates to optimize operations. One simple example is
unrolling loops. I'm sure your matrix multiplications would benefit from
this. I recommend reading C++ Templates: The Complete Guide. It's a newer
book, and it should help a lot.
 
S

Shane Neph

Andre said:
Hi,

I've built a math library that does some matrix multiplications and
other linear algebric functions. I read some place about generics or
class templates and I was wondering if this will help me in any way? I
have structures and classes with the same logic but that which deal with
different types. Moreover, I would like to know if there are any
performance benifits or drawbacks and how templates are or can be
important to numerical code (typically high-performance code). Thanks,

regards

-Andre


C++ generics will not make your code run any faster (assuming your code is
good). One of the main benefits of template programming is source code
reuse (reduced development time and being able to reuse the code in more
general cases).
There are a couple of possible drawbacks if templates are overused (ie;
instantiating (literally) a million different types using the same source
code still means a million different types --> long compile times and code
bloat - but if you need a million different types, however, then by all
means, let the compiler do the work for you). For most applications,
however, the potential drawbacks are insignifcant - you should just be aware
of them.

If used effectively, templates will not have any runtime drawbacks
relative to hand-coded loops, etc. In particular, I'm referring to generic
algorithms at this point (like transform, for_each, merge, sort, etc).
Compile-time mathematics using templates is primitive allowing only for
integers --> no arbitrary precision and usually limited to numbers < 2^32-1,
for example. I would not expect compile-time addition/subtraction to be of
substantial use for your library.

Generic programming is a way of thinking that is very powerful and
versatile if done correctly. But, it does not make your code faster than a
well-written hand-crafted alternative - it makes it about the same. It's
biggest advantages (in my own opinion) is source code reuse and abstracting
ideas in such a way to allow for easy additions/modifications later.

Good Luck.
Shane
 
G

Govindan

Shane Neph said:
C++ generics will not make your code run any faster (assuming your code is
good). One of the main benefits of template programming is source code
reuse (reduced development time and being able to reuse the code in more
general cases).
There are a couple of possible drawbacks if templates are overused (ie;
instantiating (literally) a million different types using the same source
code still means a million different types --> long compile times and code
bloat - but if you need a million different types, however, then by all
means, let the compiler do the work for you). For most applications,
however, the potential drawbacks are insignifcant - you should just be aware
of them.

If used effectively, templates will not have any runtime drawbacks
relative to hand-coded loops, etc. In particular, I'm referring to generic
algorithms at this point (like transform, for_each, merge, sort, etc).
Compile-time mathematics using templates is primitive allowing only for
integers --> no arbitrary precision and usually limited to numbers < 2^32-1,
for example. I would not expect compile-time addition/subtraction to be of
substantial use for your library.

Generic programming is a way of thinking that is very powerful and
versatile if done correctly. But, it does not make your code faster than a
well-written hand-crafted alternative - it makes it about the same. It's
biggest advantages (in my own opinion) is source code reuse and abstracting
ideas in such a way to allow for easy additions/modifications later.

Good Luck.
Shane

Using Templates may cause code bloat. Actual amounts will depend on the STL
library you use.
As long u minimize use of virtual stuff and inheritance, there wont be much
performance drawbacks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top