这适用于 .NET。
我正在寻找编写空间映射应用程序。内存中将同时有几个多边形(大约 30 - 50 个多边形)。每个 polgon 都有一组 LatLong 点。每个多边形的集合范围从 10 到 200。但是,将使用这些点进行很多计算,这就是为什么(为了性能)我想让 LatLong 成为一个结构体。但是,我对内存中的大量 LatLong 感到厌烦。对此的任何见解将不胜感激。
回顾一下:我想知道我是否应该将 LatLong 设为结构,因为我想要计算的性能,或者因为内存中的 latLong 数量为一个类。
最佳答案
这个问题的答案将取决于您的资源优先级、类/结构中的内容以及您如何使用它。我建议你弄清楚你的内存/性能资源是什么样的,然后对两种实现进行大量测试,看看它们如何适应这些资源参数。如果可能,请尝试测试您希望执行的实际操作。
MSDN also offers some good guidance on when and when not to use structs.从文章:
Do not define a structure unless the type has all of the following characteristics:
- It logically represents a single value, similar to primitive types (integer, double, and so on).
- It has an instance size smaller than 16 bytes.
- It is immutable.
- It will not have to be boxed frequently.
关于.net - 结构与类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5487511/