简体   繁体   English

存储5个属性的最有效方法

[英]Most efficient way to store 5 attributes

So I'm trying to store 5 attributes of an object, which are 5 different integers. 所以我试图存储一个对象的5个属性,它们是5个不同的整数。

What would be the best way to store these? 存储这些的最佳方法是什么? I was thinking of arrays, but arrays aren't flexible. 我当时在考虑数组,但是数组并不灵活。 I also need to be able to retrieve all 5 attributes, so arrays probably won't work well. 我还需要能够检索所有5个属性,因此数组可能无法正常工作。

Here's some background if it helps: I am currently making a game similar to Terraria (or Minecraft in 2D). 如果有帮助,这里有一些背景知识:我目前正在制作类似于Terraria(或2D的Minecraft)的游戏。 I wanted to store where the object is on the map(x,y) , where it is on the screen at the part of the map(x,y) , and what type of object it is. 我想存储对象在map(x,y) ,在屏幕上map(x,y) ,以及对象的类型。

import java.awt.Point

public class MyClass {
    private Point pointOnMap;
    private Point pointOnScreen;
    // ... 
}

The Point class binds x & y values into a single object (which makes sense) and gives you useful, basic methods it sounds like you'll need, such as translate and distance . Point类将x和y值绑定到一个对象中(这很有意义),并为您提供有用的基本方法,听起来像是您需要的,例如translatedistance http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

It is not possible to predict what is the most efficient way to store the attributes without seeing all of your code. 如果不查看所有代码,就无法预测什么是存储属性的最有效方法。 (And I for one don't want to :-)) Second, you haven't clearly explained what you are optimizing for. (而且我不想这样做:-))其次,您没有明确说明要优化的内容。 Speed? 速度? Memory usage? 内存使用情况? Minimization of GC pauses? 最大限度地减少GC暂停时间?

However, this smells of premature optimization. 但是,这闻到了过早的优化。 Wasting lost of time trying to optimize performance on something that hasn't been built, and without any evidence that the performance of this part the codebase is going to be significant. 浪费时间来优化尚未构建的产品的性能,并且没有任何证据证明该部分的性能将是重要的。

My advice would be: 我的建议是:

  1. Pick a simple design and implement it; 选择一个简单的设计并实施它; eg 5 private int variables with getters and setters. 例如5个带有getter和setter的private int变量。 If that is inconvenient , then choose a more convenient API. 如果不方便 ,请选择一个更方便的API。

  2. Complete the program. 完成程序。

  3. Get it working. 使它工作。

  4. Benchmark it. 进行基准测试。 Does it run fast enough? 运行足够快吗? If yes, stop. 如果是,请停止。

  5. Profile it. 剖析它。 Pick the biggest performance hotspot and optimize that. 选择最大的性能热点并对其进行优化。

  6. Rerun the benchmarking and profile to check that your optimization has made things faster . 重新运行基准测试和配置文件,以检查优化是否使速度更快 If yes, then "commit" it. 如果是,则“提交”它。 If not then back it out. 如果没有,则将其退出。

  7. Go to step 4. 转到步骤4。

I would suggest HashMap where key can be objectId-attributeName and value will be integer value as you have to do retrieval based on key. 我建议HashMap,其中键可以是objectId-attributeName而值将是integer因为您必须基于键进行检索。 This will be O(1) operation 这将是O(1)操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM