问题描述
嗯。我有一个表,该表是我需要存储在Java中的结构数组。天真的不用担心内存方法就是这样做的:
Hmmm. I have a table which is an array of structures I need to store in Java. The naive don't-worry-about-memory approach says do this:
public class Record {
final private int field1;
final private int field2;
final private long field3;
/* constructor & accessors here */
}
List<Record> records = new ArrayList<Record>();
如果我最终使用了大量(> 10 )记录,一次只偶尔访问一条记录,我如何才能将前面的方法(ArrayList)与优化的存储成本方法进行比较:
If I end up using a large number (> 10 ) of records, where individual records are accessed occasionally, one at a time, how would I figure out how the preceding approach (an ArrayList) would compare with an optimized approach for storage costs:
public class OptimizedRecordStore {
final private int[] field1;
final private int[] field2;
final private long[] field3;
Record getRecord(int i) { return new Record(field1[i],field2[i],field3[i]); }
/* constructor and other accessors & methods */
}
编辑:
- 假设记录的数量很少或永远不会更改
- 我可能不会使用
- 很明显,如果我在上面的OptimizedRecordStore方法中添加/更改了记录数,我会很自信地做出决定。
- 要么必须用一个新对象替换整个对象,要么删除 final关键字。
- kd304提出了一个在我脑海中浮现的优点。在与此类似的其他情况下,我需要对记录进行列访问,例如如果field1和field2是时间和位置,对我来说重要的是将这些值作为数组与MATLAB一起使用,这样我就可以有效地对其进行图形化/分析。
- assume the # of records is something that is changed infrequently or never
- I'm probably not going to use the OptimizedRecordStore approach, but I want to understand the storage cost issue so I can make that decision with confidence.
- obviously if I add/change the # of records in the OptimizedRecordStore approach above, I either have to replace the whole object with a new one, or remove the "final" keyword.
- kd304 brings up a good point that was in the back of my mind. In other situations similar to this, I need column access on the records, e.g. if field1 and field2 are "time" and "position", and it's important for me to get those values as an array for use with MATLAB, so I can graph/analyze them efficiently.
推荐答案
在这种情况下,给出一般必须时进行优化的答案是无济于事的,因为恕我直言,程序员应该始终意识到当不同的设计选择导致性能下降的数量级时,尤其是API编写器,其性能会有所不同。
The answers that give the general "optimise when you have to" is unhelpful in this case because , IMHO, programmers should always be aware of the performance in different in design choices when that choice leads to an order of magnitude performance penalty, particularly API writers.
最初的问题很有效,鉴于他的特殊情况,我倾向于同意第二种方法更好。我编写了图像处理代码,其中每个像素都需要一个数据结构,除了我需要频繁地随机访问每个像素之外,情况与此不太相似。为每个像素创建一个对象的开销很大。
The original question is quite valid and I would tend to agree that the second approach is better, given his particular situation. I've written image processing code where each pixel requires a data structure, a situation not too dissimilar to this, except I needed frequent random access to each pixel. The overhead of creating one object for each pixel was enormous.
这篇关于结构数组还是数组结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!