gpt4 book ai didi

c++ - 我应该用 C++ 手动写出 "sub-results"吗?

转载 作者:行者123 更新时间:2023-11-30 03:15:14 25 4
gpt4 key购买 nike

在我的一段代码中,我从 3 个 const vec3 计算了一个 mat4:

const vec3 position{...}, orientation{...}, scale{...};

float mat[4][4]
{
{
(2 * scale.x * cos(orientation.y + orientation.z) + scale.x * cos(orientation.x + orientation.y + orientation.z) - scale.x * cos(orientation.x - orientation.y + orientation.z) + 2 * scale.x * cos(orientation.y - orientation.z) - scale.x * cos(orientation.x + orientation.y - orientation.z) + scale.x * cos(orientation.x - orientation.y - orientation.z)) / 4,
(-scale.x * cos(orientation.x + orientation.y) + scale.x * cos(orientation.x - orientation.y)) / 2,
(-2 * scale.x * sin(orientation.y + orientation.z) - scale.x * sin(orientation.x + orientation.y + orientation.z) + scale.x * sin(orientation.x - orientation.y + orientation.z) + 2 * scale.x * sin(orientation.y - orientation.z) - scale.x * sin(orientation.x + orientation.y - orientation.z) + scale.x * sin(orientation.x - orientation.y - orientation.z)) / 4,
0
},
{
(-scale.y * cos(orientation.x + orientation.z) + scale.y * cos(orientation.x - orientation.z)) / 2,
scale.y * cos(orientation.x),
(scale.y * sin(orientation.x + orientation.z) + scale.y * sin(orientation.x - orientation.z)) / 2,
0
},
{
(2 * scale.z * sin(orientation.y + orientation.z) + scale.z * sin(orientation.x + orientation.y + orientation.z) + scale.z * sin(orientation.x - orientation.y + orientation.z) + 2 * scale.z * sin(orientation.y - orientation.z) - scale.z * sin(orientation.x + orientation.y - orientation.z) - scale.z * sin(orientation.x - orientation.y - orientation.z)) / 4,
(-scale.z * sin(orientation.x + orientation.y) - scale.z * sin(orientation.x - orientation.y)) / 2,
(2 * scale.z * cos(orientation.y + orientation.z) + scale.z * cos(orientation.x + orientation.y + orientation.z) + scale.z * cos(orientation.x - orientation.y + orientation.z) - 2 * scale.z * cos(orientation.y - orientation.z) + scale.z * cos(orientation.x + orientation.y - orientation.z) + scale.z * cos(orientation.x - orientation.y - orientation.z)) / 4,
0
},
{
(-2 * position.y * cos(orientation.x + orientation.z) + 2 * position.x * cos(orientation.y + orientation.z) + position.x * cos(orientation.x + orientation.y + orientation.z) - position.x * cos(orientation.x - orientation.y + orientation.z) + 2 * position.y * cos(orientation.x - orientation.z) + 2 * position.x * cos(orientation.y - orientation.z) - position.x * cos(orientation.x + orientation.y - orientation.z) + position.x * cos(orientation.x - orientation.y - orientation.z) + 2 * position.z * sin(orientation.y + orientation.z) + position.z * sin(orientation.x + orientation.y + orientation.z) + position.z * sin(orientation.x - orientation.y + orientation.z) + 2 * position.z * sin(orientation.y - orientation.z) - position.z * sin(orientation.x + orientation.y - orientation.z) - position.z * sin(orientation.x - orientation.y - orientation.z)) / 4,
(2 * position.y * cos(orientation.x) - position.x * cos(orientation.x + orientation.y) + position.x * cos(orientation.x - orientation.y) - position.z * sin(orientation.x + orientation.y) - position.z * sin(orientation.x - orientation.y)) / 2,
(2 * position.z * cos(orientation.y + orientation.z) + position.z * cos(orientation.x + orientation.y + orientation.z) + position.z * cos(orientation.x - orientation.y + orientation.z) - 2 * position.z * cos(orientation.y - orientation.z) + position.z * cos(orientation.x + orientation.y - orientation.z) + position.z * cos(orientation.x - orientation.y - orientation.z) + 2 * position.y * sin(orientation.x + orientation.z) - 2 * position.x * sin(orientation.y + orientation.z) - position.x * sin(orientation.x + orientation.y + orientation.z) + position.x * sin(orientation.x - orientation.y + orientation.z) + 2 * position.y * sin(orientation.x - orientation.z) + 2 * position.x * sin(orientation.y - orientation.z) - position.x * sin(orientation.x + orientation.y - orientation.z) + position.x * sin(orientation.x - orientation.y - orientation.z)) / 4,
1
}
};

如您所见,计算仅由几个简单的操作组成(+-*/, sincos), 但其中很多出现了很多次。此类操作的示例是:

orientation.y + orientation.z
orientation.x + oy_plus_oz
sin(orientation.x - oy_plus_oz)
//many more...

我不希望处理器多次计算它们,所以我尝试用“shortucts”替换它们:

float oy_plus_oz = orientation.y + orientation.z;
float cos_oy_plus_oz = cos(oy_plus_oz);
float ox_plus_oy_plus_oz = orientation.x + oy_plus_oz;
float ox_minus_oy_plus_oz = orientation.x - oy_plus_oz;
float oy_minus_oz = orientation.y - orientation.z;
float ox_plus_oy_minus_oz = orientation.x + oy_minus_oz;
float ox_minus_oy_minus_oz = orientation.x - oy_minus_oz;
float cos_ox_plus_oy_plus_oz = cos(ox_plus_oy_plus_oz);
float cos_ox_minus_oy_plus_oz = cos(ox_minus_oy_plus_oz);
float two_mult_sx = 2 * scale.x;
float cos_oy_minus_oz = cos(oy_minus_oz);
float cos_ox_plus_oy_minus_oz = cos(ox_plus_oy_minus_oz);
float cos_ox_minus_oy_minus_oz = cos(ox_minus_oy_minus_oz);
float ox_plus_oy = orientation.x + orientation.y;
float ox_minus_oy = orientation.x - orientation.y;
float cos_ox_plus_oy = cos(ox_plus_oy);
float cos_ox_minus_oy = cos(ox_minus_oy);

是否有更好/更快的方法,或者完全没有必要?关键是这段代码应该尽可能快(避免多次不必要地计算相同的值) .

最佳答案

Is there a better/faster way to do it, or is it unnecessary altogether? The key is that this code should be as fast as possible (avoid unnecessary computation of the same value multiple times).

优化编译器将寻找计算速度更快的等效表达式,包括分解子表达式。

如果不查看生成的代码并测量实际执行时间,就无法确定哪种方式最终会产生最好的代码(最佳解决方案可能取决于您的 objective-c PU)。

但是,一般来说,首先计算重复表达式通常对人类来说更清晰,有助于调试,也可以简化优化器的工作。

此外,由于这是关于浮点计算的,因此顺序很重要,因此仔细检查传递给优化器的构建标志非常重要。

关于c++ - 我应该用 C++ 手动写出 "sub-results"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57226720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com