On Monday 08 May 2006 06:05, Brian Budge wrote:
Hi all -
I just tried out the operators library for the first time today. I was quite happy with how easily it helped me implement my test class. Unfortunately, I'm running into some performance issues, in particular with operator +.
The interesting thing is that the new operator += is actually faster than my old += by about a factor of 2, but my operator + is slower than my old + by a factor of nearly 10!
I'm new here, and not really sure how much code is appropriate to post to the list, so here's a minimal rundown:
-------------------------------------------------------------------------- Old code:
template<typename real> Vector3<real> operator + (const Vector3<real> &a, const Vector3<real> &b);
template<typename real> class Vector3 { protected: real m_v[3]; . . friend Vector3 operator +<> (const Vector3 &a, const Vector3 &b); };
template<typename real> inline Vector3<real> operator + (const Vector3<real> &a, const Vector3<real> &b){ Vector3<real> t; t[0] = a[0] + b[0]; t[1] = a[1] + b[1]; t[2] = a[2] + b[2]; return t; }
--------------------------------------------------------------------------- ---------------------- New code (note that the new code is templatized on size, and that my instantiations were of size 3 to match the above code):
template
class vec : boost::additive< vec
, boost::multiplicative< vec
, T { private: typedef unsigned int uint; typedef const T &const_reference; typedef T &reference;
private: boost::array
m_v; template<typename U> vec &operator += (const vec &t){ for(uint i = 0; i < S; ++i){ m_v[i] += t.m_v[i]; } return *this; } };
Any ideas how to increase the performance of the new code here? A factor of 10 makes it seem like I am just missing something important.
I don't know anything about the operators library, but I noticed that the fast implementation does not have the loop that the slow one has. Loops are typically long to setup, so that could be the problem. Fred