Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory comparison, which is faster?

Tags:

c++

comparison

I have a 3D vector class. The private variables are defined:

union {
    struct {
        double x;
        double y;
        double z;
    };
    double data[3];
};

In implementing operator==, which is faster?

return this->x == v.x && this->y == v.y && this->z == v.z;

OR

return memcmp(this->data, v.data) == 0;
like image 513
Anthony Avatar asked Jun 22 '10 03:06

Anthony


1 Answers

Unfortunately the two aren't equivalent. (Specifically NaNs and signed zeros don't use bitwise comparison inside the FPU).

So you should make your choice based on correctness, not speed.

like image 105
Ben Voigt Avatar answered Nov 03 '22 00:11

Ben Voigt