In C++, i'm trying to write a wrapper around a 64 bits integer. My expectation is that if written correctly and all methods are inlined such a wrapper should be as performant as the real type. Answer to this question on SO seems to agree with my expectation.
I wrote this code to test my expectation :
class B
{
private:
uint64_t _v;
public:
inline B() {};
inline B(uint64_t v) : _v(v) {};
inline B& operator=(B rhs) { _v = rhs._v; return *this; };
inline B& operator+=(B rhs) { _v += rhs._v; return *this; };
inline operator uint64_t() const { return _v; };
};
int main(int argc, char* argv[])
{
typedef uint64_t;
//typedef B T;
const unsigned int x = 100000000;
Utils::CTimer timer;
timer.start();
T sum = 0;
for (unsigned int i = 0; i < 100; ++i)
{
for (uint64_t f = 0; f < x; ++f)
{
sum += f;
}
}
float time = timer.GetSeconds();
cout << sum << endl
<< time << " seconds" << endl;
return 0;
}
When I run this with typedef B T
; instead of typedef uint64_t T
the reported times are consistently 10% slower when compiled with VC++. With g++ the performances are same if I use the wrapper or not.
Since g++ does it I guess there is no technical reason why VC++ can not optimise this correctly. Is there something I could do to make it optimize it?
I already tried to play with the optimisations flag with no success