内存比较,这比较快? [英] Memory comparison, which is faster?

查看:94
本文介绍了内存比较,这比较快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D向量类。私有变量被定义:

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

在实现运算符==,哪个更快?

 
return this-> x == vx && this-> y == vy && this-> z == vz;



 
return memcmp > data,v.data)== 0;


解决方案

不幸的是,两者不等同。 (特别是NaN和有符号的零不使用FPU内部的逐位比较)。



所以你应该根据正确性而不是速度来选择。 b $ b

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.

这篇关于内存比较,这比较快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆