使用 Tuple(double,int,int) 的数组列表比两个数组列表慢

发布于 2024-11-27 02:42:37 字数 493 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烂柯人 2024-12-04 02:42:37

你的问题缺少上下文。这个问题已经被问过很多次了,并且没有一个最佳的解决方案。

在我看来,对数据进行建模的最佳方法是拥有代表数据的逻辑类型。 (您当前正在使用元组,但最好有一个带有方法的特定类型。)

因此,我会执行以下操作:

List<NumberContainer> list = new ArrayList<NumberContainer>();

特别是就速度而言 - 这取决于您将如何使用数据。如果您寻求快速访问时间,最好使用地图并为每个项目指定某个值。

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.

执着的年纪 2024-12-04 02:42:37

除非您编写了一个自定义 Tuple 类,该类维护一个未装箱 double 和两个 int 值,否则它们将无论如何都要装箱...所以基本上你最终会得到每一项额外的 Tuple 对象,尽管只有一个底层数组和 ArrayList 而不是 3。

如果值代表一个有意义的复合值,但我很想写一个小类来封装这三个属性,并为每个属性指定有意义的名称。这样您可能最终会得到更易读的代码和更高效的代码(因为不会有任何装箱)。

Unless you've written a custom Tuple class which maintain an unboxed double 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.

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).

谁人与我共长歌 2024-12-04 02:42:37

最有可能使用对象数组(或者在您的情况下为元组),这将为您节省一行代码,并将所有内容放在一个地方(元组)。

这是我要做的示例代码。

//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>();

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>();
眼眸印温柔 2024-12-04 02:42:37

如果 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.

離殇 2024-12-04 02:42:37

为了回答您的直接问题,方法2确实通过自动装箱创建对象,假设您放入的值是基元(doubleint等) .)。当然,如果您使用 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 two ints and a double.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文