java 使用 Tuple(double,int,int) 的数组列表比两个数组列表慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6863194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is using an arraylist of Tuple(double,int,int) slower than two arraylists
提问by Wei Shi
Is using an arraylist of Tuple(double,int,int) slower than three separate arraylists? I want to avoid creating lots of Tuple objects, but does method 2 create objects by autoboxing?
使用 Tuple(double,int,int) 的数组列表是否比三个单独的数组列表慢?我想避免创建大量的元组对象,但是方法 2 是否通过自动装箱创建对象?
//Method 1
Arraylist<Tuple> arr=new Arraylist<Tuple>();
Tuple t=new Tuple(double, int, int);
class Tuple{
private double value;
private int a;
private int b;
}
//Method 2
Arraylist<Double> arr=new Arraylist<Double>();
Arraylist<Integer> arr=new Arraylist<Integer>();
Arraylist<Integer> arr=new Arraylist<Integer>();
采纳答案by jjnguy
Your question is missing context. This problem has been asked many times, and there is no single best solution.
您的问题缺少上下文。这个问题已经被问过很多次了,没有一个最好的解决方案。
In my opinion, the best way to model the data is to have a logical type that represents your data. (You are currently using a tuple, but it would be better to have a specific type with methods.)
在我看来,对数据建模的最佳方法是拥有一个代表您的数据的逻辑类型。(您目前正在使用元组,但最好使用带有方法的特定类型。)
So, I would do the following:
所以,我会做以下事情:
List<NumberContainer> list = new ArrayList<NumberContainer>();
As far as speed goes in particular - It depends on how you are going to use the data. If you are looking for fast access times, it may be best to use a map
and key each item on some value.
就速度而言,特别是 - 这取决于您将如何使用数据。如果您正在寻找快速访问时间,最好使用 amap
并在某个值上键入每个项目。
回答by Jon Skeet
Unless you've written a custom Tuple
class which maintain an unboxeddouble
and two int
values, they'll be boxed anyway... so basically you'll end up with the extra Tuple
object per item, although just one underlying array and ArrayList
instead of 3.
除非你编写了一个自定义Tuple
类来维护一个未装箱的值double
和两个int
值,否则它们无论如何都会被装箱......所以基本上你最终会得到Tuple
每个项目的额外对象,尽管只有一个底层数组而ArrayList
不是 3。
If the triple of values represents a meaningful composite value though, I'd be very tempted to write a small class to encapsulate the three of them with meaningful namesfor each property. That way you're likely to end up with more readable code andefficient code (as there won't be any boxing).
如果三个值代表一个有意义的复合值,我会很想写一个小类来用每个属性的有意义的名称封装这三个。这样你很可能会得到更易读的代码和高效的代码(因为不会有任何装箱)。
回答by gammaraptor
Most likely using an array of objects (or in your case, tuples) which would save you a line of code, and put everything in one place (the tuple.)
最有可能使用一组对象(或者在您的情况下是元组),这可以为您节省一行代码,并将所有内容放在一个地方(元组。)
Here's the sample code for what I would do.
这是我要做的示例代码。
//Class
class container() {
int value1, value2;
double value3;
//Constructor
container(int value1, int value2, double value3) {
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
}
//Implementation
ArrayList<container> arr=new ArrayList<container>();
回答by Oscar Gomez
If Tople is a class with 3 ivars, in that case that would be the way to go.
如果 Tople 是一个有 3 个 ivars 的类,那么这就是要走的路。
Aditionaly arralist only take objects so it will autobox all the primitives, but if you are using a class it will definitly not autobox the ivars in the class.
另外 arralist 只接受对象,所以它会自动装箱所有的基元,但如果你使用一个类,它肯定不会自动装箱类中的 ivars。
回答by Gravity
To answer your direct question, method2 doescreate objects by autoboxing assuming that the values you're putting in are primitives (double
, int
, etc.). Of course, if you use a Tuple class, you're also creating objects, but you will be creating 1/3 the number of objects, assuming the Tuple class maintains two int
s and a double
.
要回答你直接问,方法2不通过自动装箱假设你投入的值是原语(创建对象double
,int
等)。当然,如果您使用 Tuple 类,您也在创建对象,但您将创建对象数量的 1/3,假设 Tuple 类维护两个int
s 和 a double
。