开发者

Memory comparison, which is faster?

开发者 https://www.devze.com 2023-01-04 02:18 出处:网络
I have a 3D vector class. The private variables are defined: union { struct { double x; double y; double z;

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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消