Bar*_*klı 4 c++ directx direct3d xna-math-library direct3d11
使用新的XMVECTOR和XMFLOAT3类,获得2点之间距离的最佳方法是什么?我找不到在XMVector*系列函数中执行此操作的函数,因此我想出了以下内容:
float distance(const XMFLOAT3& v1,const XMFLOAT3& v2)
{
XMVECTOR vector1 = XMLoadFloat3(&v1);
XMVECTOR vector2 = XMLoadFloat3(&v2);
XMVECTOR vectorSub = XMVectorSubtract(vector1,vector2);
XMVECTOR length = XMVector3Length(vectorSub);
float distance = 0.0f;
XMStoreFloat(&distance,length);
return distance;
}
Run Code Online (Sandbox Code Playgroud)
这会比普通的Vector3类更快,只有3个浮点数用于x,y,z,然后使用sqrt,因为它使用内在优化?即:
float Distance(const Vector3& point1,const Vector3& point2)
{
float distance = sqrt( (point1.x - point2.x) * (point1.x - point2.x) +
(point1.y - point2.y) * (point1.y - point2.y) +
(point1.z - point2.z) * (point1.z - point2.z) );
return distance;
}
Run Code Online (Sandbox Code Playgroud)
只有一种方法可以获得点之间的距离.这就是你描述的方式.vec3 diff = b - a; float distance = sqrtf(dot(diff, diff));
这会比普通的Vector3类更快,只有3个浮点数用于x,y,z,然后使用sqrt,因为它使用内在优化?
如果您担心性能问题,则需要对应用程序进行分析,而不是试图猜测什么会更快."低效"解决方案的影响很有可能在您的应用程序中完全无法察觉.
还有一个非常好的机会,如果你自己开始编写例程,你将使它们效率降低或引入错误.例如,您的"手写"代码可能(取决于编译器)执行比使用"XMVECTOR"的原始代码更多的浮点计算.在您的例程中,您计算两次向量之间的差异.如果编译器没有对其进行优化,那么最终会有6次减法,2次加法,1次sqrtf和3次乘法.在"XMVECTOR"代码中,您将使用3次减法,2次加法,3次乘法和1次sqrtf.
对于分析应用程序,您可以使用AQTime 7 Standard(目前是免费的)或gprof(如果您使用gcc/g ++进行编译).
| 归档时间: |
|
| 查看次数: |
10571 次 |
| 最近记录: |