[英]Java: String vs. custom class for storing a point
I am dealing with a large set of X,Y,Z points. 我正在处理一大堆X,Y,Z点。 What will be the best way to store all of the points if I need to keep RAM usage low? 如果我需要保持较低的RAM使用率,那么存储所有点的最佳方法是什么? A string "X,Y,Z", a custom class, or would it not make any difference either way? 一个字符串“X,Y,Z”,一个自定义类,或者它不会有任何区别?
Strings are almost definitely a wrong choice: they waste about half the space, if not more, and they make you waste CPU cycles to get the data back into the numeric format suitable for processing. 字符串几乎肯定是一个错误的选择:它们浪费了大约一半的空间,如果不是更多,它们会让你浪费CPU周期来将数据恢复为适合处理的数字格式。
A custom class with three primitive fields is a much better choice. 具有三个原始字段的自定义类是更好的选择。 Depending on the range and the type of your points, use byte
, short
, int
, long
, float
, or double
. 根据点的范围和类型,使用byte
, short
, int
, long
, float
或double
。 This helps you avoid parsing the coordinates every time you are about to use your points. 这有助于您在每次使用积分时避免解析坐标。
为什么不包含3个元素的数组?
You can make one array to store X, Y, Z together, if you want to achieve low memory usage. 如果要实现低内存使用,可以将一个数组存储在一起,以存储X,Y,Z。
For a point with index i you can store X, Y, Z in array elements with following indexes: 对于索引为i的点,可以将X,Y,Z存储在具有以下索引的数组元素中:
Edit: This approach also has the following important advantages, which might in some circumstances be very handy: 编辑:这种方法还具有以下重要优点,在某些情况下可能非常方便:
You can of course wrap this big table with a class with methods like getX(int index)
to make it more readable for a little CPU cost. 您当然可以使用类似getX(int index)
的类来包装这个大表,以使其更具可读性,从而降低CPU成本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.