使用 Tuple(double,int,int) 的数组列表比两个数组列表慢
使用 Tuple(double,int,int) 的数组列表是否比三个单独的数组列表慢?我想避免创建大量 Tuple 对象,但是方法 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>();
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?
//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>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的问题缺少上下文。这个问题已经被问过很多次了,并且没有一个最佳的解决方案。
在我看来,对数据进行建模的最佳方法是拥有代表数据的逻辑类型。 (您当前正在使用元组,但最好有一个带有方法的特定类型。)
因此,我会执行以下操作:
特别是就速度而言 - 这取决于您将如何使用数据。如果您寻求快速访问时间,最好使用
地图
并为每个项目指定某个值。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:
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.除非您编写了一个自定义
Tuple
类,该类维护一个未装箱double
和两个int
值,否则它们将无论如何都要装箱...所以基本上你最终会得到每一项额外的Tuple
对象,尽管只有一个底层数组和ArrayList
而不是 3。如果值代表一个有意义的复合值,但我很想写一个小类来封装这三个属性,并为每个属性指定有意义的名称。这样您可能最终会得到更易读的代码和更高效的代码(因为不会有任何装箱)。
Unless you've written a custom
Tuple
class which maintain an unboxeddouble
and twoint
values, they'll be boxed anyway... so basically you'll end up with the extraTuple
object per item, although just one underlying array andArrayList
instead of 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 names for each property. That way you're likely to end up with more readable code and efficient code (as there won't be any boxing).
最有可能使用对象数组(或者在您的情况下为元组),这将为您节省一行代码,并将所有内容放在一个地方(元组)。
这是我要做的示例代码。
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.
如果 Tople 是一个包含 3 个 ivars 的类,那么这就是正确的选择。
另外,arralist 只接受对象,因此它会自动装箱所有基元,但如果您使用的是类,它肯定不会自动装箱类中的 ivars。
If Tople is a class with 3 ivars, in that case that would be the way to go.
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.
为了回答您的直接问题,方法2确实通过自动装箱创建对象,假设您放入的值是基元(
double
、int
等) .)。当然,如果您使用 Tuple 类,您也会创建对象,但您将创建 1/3 的对象数量,假设 Tuple 类维护两个int
和一个双
。To answer your direct question, method2 does create 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 twoint
s and adouble
.