Skip to main content
gamedev.net
🔒 Locked

expression templates

Started by incin 2 replies 600+ views
Original Post
incin
incin
I thought it would be useful to make my own 3d vector implementation using expression templates. With my simple tests comparing my '+' operation only vector, it seems that the naive approach to a vector implementation may be just as good with an optimizing compiler. The test I use now is the most simple of the simple, but do you expect an expression template vector to out-perform a simple vector implementation. One that does:

class Badvec { 
...
      Badvec operator+(Badvec& aAdd)
      {
         Badvec tmp;
         tmp.x = x + aAdd.x;
         tmp.y = y + aAdd.y;
         tmp.z = z + aAdd.z;
         return tmp;
      }
...
}

Emmanuel Deloget
Emmanuel Deloget
Probably. See this. The trick is that expressin templates can be wired very late in the development, so if you are at the very beginning of your project, I'd say that you don't have to worry yet.

Moreover, if you are not sure about performance, what about testing both versions by yourself?

Regards,
snk_kid
snk_kid
@incin it's pointless using expressions templates for 2/3/4 vectors/matrices, only user-defined operators of arbitrary/N sized vectors/matricies benefit from expression templates because of the cost of temporaries/copies. This was their original intent, now it's also used in the making of domain specific embedded languages (DESLs) in C++ as well their original intent.

Quote:
Original post by Anonymous Poster
Quote:
Original post by incin
I thought it would be useful to make my own 3d vector implementation using expression templates. With my simple tests comparing my '+' operation only vector, it seems that the naive approach to a vector implementation may be just as good with an optimizing compiler. The test I use now is the most simple of the simple, but do you expect an expression template vector to out-perform a simple vector implementation. One that does:

class Badvec {
...
Badvec operator+(Badvec& aAdd)
{
Badvec tmp;
tmp.x = x + aAdd.x;
tmp.y = y + aAdd.y;
tmp.z = z + aAdd.z;
return tmp;
}
...
}



I suspect so but try testing against following instead, if you are worried by performance.

*** Source Snippet Removed ***


The best method of implementing arithmetic operators is to define them in terms of arithmetic assignment operators i.e.:

struct vector3 {      float x, y, z;   vector3& operator+=(const vector3& that) {         x += that.x;       y += that.y;       z += that.z;       return *this;   }};inline vector3 operator+(const vector3& lhs, const vector3& rhs)  {     return vector3(lhs) += rhs;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.