结构和类之间决定重新present六面体在方案里会有数以百万计人的记忆 [英] Decide between struct and class to represent a hexahedron in a scenario where there will be millions of them in memory

查看:156
本文介绍了结构和类之间决定重新present六面体在方案里会有数以百万计人的记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对.NET 4.5的pretty的不平凡的设计问题。我是应该有几百万的六面体网格。每一个六面体有8分和6个四边形面。每个四边形面可以是平面或弯曲的。如果它是平面的,它是由一个psented重新$ P $(类|结构)称为基准面,具有4双打为平面方程,并具有4个顶点的四边形。如果脸部是弯曲的,它是由一个单一的点和一个3×3矩阵psented重新$ P $。

I have a pretty non trivial design problem on .net 4.5. I have a grid that is supposed to have millions of hexahedrons. Each hexahedron has 8 points and 6 quadrilateral faces. Each quadrilateral face may be planar or curved. If it is planar, it is represented by a (class|struct) called Plane, that has 4 doubles for the plane equation and has the 4 vertices of the quadrilateral. If the face is Curved, it is represented by a single point and a 3x3 matrix.

在这里主要关注的是性能,垃圾收集,假定2GB的内存限制为块的任何数组。现在的问题是:我们有座,点,面,平面,曲线,MATRIX3X3。这其中应该是一流的,哪些人应该结构体?

The main concern here is performance, garbage collection, assuming a memory limit of 2gb for any array of blocks. The question is: we have Block, Point, Face, Plane, Curve, Matrix3x3. Which of them should be class and which of them should be struct?

推荐答案

(忽略的P / Invoke的方面,这是一个不同的问题)

(Ignoring P/Invoke aspects, which is a different matter)

作为一个非常一般的经验法则,你应该只让种少量的数据(例如32字节)为结构。

As a very general rule of thumb, you should only make types with small amounts of data (say 32 bytes) into structs.

请注意,结构最好应该是一成不变的。

Note that structs should ideally be immutable.

在速度方面:这取决于你在做什么,所以你就必须执行一些定时真正知道。但是,很可能,当你传递物品的方法,将更快地传递引用类型,而不是一个结构类型时,该结构的大小大于参考尺寸(这将是32位的32位code 64位64位code)。

In terms of speed: It depends what you're doing, so you would have to perform some timings to really tell. However, it's likely that when you are passing items to a method it will be quicker to pass a reference type rather than a struct type when the struct size is greater than the reference size (which will be 32 bits for 32 bit code and 64 bits for 64 bit code).

记住创建数组或列表的时候要承担一个非常重要的事情:对于值类型,以字节为单位的时间值的大小元素的数量是底层阵列的总规模连片

One very important thing to bear in mind when creating arrays or List: For value types, the size of the value in bytes times the number of elements is the total contiguous size of the underlying array.

有关引用类型,总的大小是一个参考的大小(32位或64位)倍的阵列的大小。

For reference types, the total size is the size of a reference (32 bits or 64 bits) times the size of the array.

自阵列的最大尺寸为2 ^ 31字节,这可以是重要的,如果该值类型的大小超过的基准尺寸。

Since the maximum size of an array is 2^31 bytes, this can be important if the size of the value type exceeds the size of a reference.

这篇关于结构和类之间决定重新present六面体在方案里会有数以百万计人的记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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