Class Templates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andre

    Class Templates

    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

  • Daniel Seitz

    #2
    Re: Class Templates


    Andre wrote in message <3F26761D.90609 09@hotmail.com> ...[color=blue]
    >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
    >[/color]

    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.


    Comment

    • Shane Neph

      #3
      Re: Class Templates


      "Andre" <food_crazy@hot mail.com> wrote in message
      news:3F26761D.9 060909@hotmail. com...[color=blue]
      > 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
      >[/color]


      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




      Comment

      • Govindan

        #4
        Re: Class Templates


        "Shane Neph" <euclidonomy@ho tmail.com> wrote in message
        news:giWVa.8692 $jB5.2335@nwrdd c03.gnilink.net ...[color=blue]
        >
        > "Andre" <food_crazy@hot mail.com> wrote in message
        > news:3F26761D.9 060909@hotmail. com...[color=green]
        > > 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
        > >[/color]
        >
        >
        > 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[/color]
        aware[color=blue]
        > of them.
        >
        > If used effectively, templates will not have any runtime drawbacks
        > relative to hand-coded loops, etc. In particular, I'm referring to[/color]
        generic[color=blue]
        > 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 <[/color]
        2^32-1,[color=blue]
        > for example. I would not expect compile-time addition/subtraction to be[/color]
        of[color=blue]
        > 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[/color]
        a[color=blue]
        > well-written hand-crafted alternative - it makes it about the same. It's
        > biggest advantages (in my own opinion) is source code reuse and[/color]
        abstracting[color=blue]
        > ideas in such a way to allow for easy additions/modifications later.
        >
        > Good Luck.
        > Shane
        >
        >
        >
        >[/color]

        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.


        Comment

        Working...