D 性能:union 与 @property
我正在移植、增强和 D 化我们的 reign SDK从 C# 到 D。目前正在研究 Vector2 数学模块。
下面两个结构体之间会有性能差异吗?我的基准测试显示了相同的性能,但我想获得一些专家的见解:)
struct Vector2(T)
{
T x, y;
@property T u() { return x; }
@property T v() { return y; }
@property void u(T value) { x = value; }
@property void v(T value) { y = value; }
}
struct Vector2(T)
{
union { T x, u; }
union { T y, v; }
}
显然我想使用联合来简化语法。但使用它们是否有任何不可预见的陷阱?我不熟悉他们的底层细节。
顺便说一句,我添加了类似于 HLSL/GLSL 的向量属性语法,例如 vec1.yxz += vec2.xyz;有...没有..有可能用工会而不是@property来做到这一点吗?
I'm in the process of porting, enhancing, and D-atizing our reign SDK from C# to D. Currently working on the Vector2 math module.
Will there be any performance difference between the two structs below? My benchmarks show identical performance, but I'd like to gain a bit of expert insight :)
struct Vector2(T)
{
T x, y;
@property T u() { return x; }
@property T v() { return y; }
@property void u(T value) { x = value; }
@property void v(T value) { y = value; }
}
struct Vector2(T)
{
union { T x, u; }
union { T y, v; }
}
Obviously I'd like to use the unions for syntactical simplicity. But is there any unforeseen pitfalls with using them? I'm unfamiliar with their low-level details.
On a side note, I'm adding in vector property syntax similar to HLSL/GLSL, e.g., vec1.yxz += vec2.xyz; There's... no.. possible way to do that with unions instead of @property is there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
别名
!Use
alias
!