gpt4 book ai didi

c++ - 哪些属性对于 Point 类更有效?

转载 作者:行者123 更新时间:2023-11-30 01:20:02 25 4
gpt4 key购买 nike

好的,我想创建 Point3f 类,这是一类包含在 3D 浮点坐标中的点。

所以,我有两种简单的方法来定义类的属性:

class Point3f
{
float x;
float y;
float z;
};

class Point3f
{
float coord[3];
}

我想知道哪个(通常)更有效,尤其是对于图形程序。 IE。哪个更适合绘制点:glVertex3f() 或 glVertex3v()?
我不知道我是否正确。我认为第一个需要更多的运行时内存,另一个需要更多的 CPU 使用率。

编辑:如果我们谈论的是更复杂的类型,比如由 3 个点组成的三角形或由 3 个三角形组成的四面体 -> 属性:数组或一个接一个?

请告诉我哪个更有效,为什么!

最佳答案

您可以比较为基本功能生成的程序集(我在 OS X 10.7.4 上使用 GCC 4.8.1)

struct Point3D {
float m_data[3];
};

struct Point3Ds {
float x;
float y;
float z;
};

double dot(const Point3D& p1, const Point3D& p2) {
asm("# Dot - Point3D");
return p1.m_data[0] * p2.m_data[0] +
p1.m_data[1] * p2.m_data[1] +
p1.m_data[2] * p2.m_data[2];
}

double dot(const Point3Ds& p1, const Point3Ds&p2) {
asm("# Dot - Point3Ds");
return p1.x * p2.x +
p1.y * p2.y +
p1.z * p2.z;
}

Point3D cross(const Point3D& p1, const Point3D& p2) {
asm("# Cross - Point3D");
return { p1.m_data[1] * p2.m_data[2] - p1.m_data[2] * p2.m_data[1],
p1.m_data[2] * p2.m_data[0] - p1.m_data[0] * p2.m_data[2],
p1.m_data[0] * p2.m_data[1] - p1.m_data[1] * p2.m_data[0]};
}

Point3D cross(const Point3Ds& p1, const Point3Ds& p2) {
asm("# Cross - Point3Ds");
return { p1.y * p2.z - p1.z * p2.y,
p1.z * p2.x - p1.x * p2.z,
p1.x * p2.y - p1.y * p2.x};
}

编译为 g++ -O3 -S 我得到以下汇编程序(相关部分):

# 12 "point3f.cpp" 1
# Dot - Point3D
# 0 "" 2
movss (%rdi), %xmm0
movss 4(%rdi), %xmm1
mulss (%rsi), %xmm0
mulss 4(%rsi), %xmm1
addss %xmm1, %xmm0
movss 8(%rdi), %xmm1
mulss 8(%rsi), %xmm1
addss %xmm1, %xmm0
unpcklps %xmm0, %xmm0
cvtps2pd %xmm0, %xmm0
ret
LFE0:
.align 4,0x90
.globl __Z3dotRK8Point3DsS1_
__Z3dotRK8Point3DsS1_:
LFB1:
# 19 "point3f.cpp" 1
# Dot - Point3Ds
# 0 "" 2
movss (%rdi), %xmm0
movss 4(%rdi), %xmm1
mulss (%rsi), %xmm0
mulss 4(%rsi), %xmm1
addss %xmm1, %xmm0
movss 8(%rdi), %xmm1
mulss 8(%rsi), %xmm1
addss %xmm1, %xmm0
unpcklps %xmm0, %xmm0
cvtps2pd %xmm0, %xmm0
ret
LFE1:
.align 4,0x90
.globl __Z5crossRK7Point3DS1_
__Z5crossRK7Point3DS1_:
LFB2:
# 26 "point3f.cpp" 1
# Cross - Point3D
# 0 "" 2
movss 4(%rdi), %xmm3
movss 8(%rdi), %xmm1
movss 8(%rsi), %xmm5
movaps %xmm3, %xmm2
movss 4(%rsi), %xmm4
movaps %xmm1, %xmm0
mulss %xmm5, %xmm2
mulss %xmm4, %xmm0
subss %xmm0, %xmm2
movss (%rdi), %xmm0
mulss %xmm0, %xmm5
movss %xmm2, -24(%rsp)
movss (%rsi), %xmm2
mulss %xmm4, %xmm0
mulss %xmm2, %xmm1
mulss %xmm3, %xmm2
subss %xmm5, %xmm1
subss %xmm2, %xmm0
movss %xmm1, -20(%rsp)
movss %xmm0, -16(%rsp)
movq -24(%rsp), %xmm0
movd -16(%rsp), %xmm1
ret
LFE2:
.align 4,0x90
.globl __Z5crossRK8Point3DsS1_
__Z5crossRK8Point3DsS1_:
LFB3:
# 33 "point3f.cpp" 1
# Cross - Point3Ds
# 0 "" 2
movss 4(%rdi), %xmm3
movss 8(%rdi), %xmm1
movss 8(%rsi), %xmm5
movaps %xmm3, %xmm2
movss 4(%rsi), %xmm4
movaps %xmm1, %xmm0
mulss %xmm5, %xmm2
mulss %xmm4, %xmm0
subss %xmm0, %xmm2
movss (%rdi), %xmm0
mulss %xmm0, %xmm5
movss %xmm2, -24(%rsp)
movss (%rsi), %xmm2
mulss %xmm4, %xmm0
mulss %xmm2, %xmm1
mulss %xmm3, %xmm2
subss %xmm5, %xmm1
subss %xmm2, %xmm0
movss %xmm1, -20(%rsp)
movss %xmm0, -16(%rsp)
movq -24(%rsp), %xmm0
movd -16(%rsp), %xmm1
ret

所以程序集是相同的。但我同意将其存储为静态数组(即 float m_data[3])会更实用,因为我可以两全其美:当我需要,以及一个高级的、惯用的 xyz 通过 getter。从这个意义上说,我相信我会以类似于以下的方式实现此类:

class MyPoint3S {
public:
MyPoint3S(float x, float y, float z)
: m_data{x, y, z} { }

// the following getters will be inlined
float x() const {
return m_data[0];
}

float y() const {
return m_data[1];
}

float z() const {
return m_data[2];
}

// in case you want to use the pointer --- some would advice against
// offering a hook to a private member.
float* data() {
return m_data;
}

private:
float m_data[3];
};

像这样使用它:

MyPoint3S p(1.0f, 2.0f, 3.0f);
std::cout<<"p = "<<p.x()<<", "<<p.y()<<", "<<p.z()<<std::endl;

获取:

p = 1, 2, 3

或者以您喜欢的任何方式调用 OpenGL 函数:

glVertex3fv(p.data());

glVertex3f(p.x(), p.y(), p.z());

关于c++ - 哪些属性对于 Point 类更有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987702/

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