简体   繁体   English

在Java中高效地对多个数组列表执行操作

[英]Perform operation on multiple arraylists efficiently in Java

I have ArrayLists of doubles within an ArrayList. 我在ArrayList中有双打的ArrayList。 So it is something like this 所以是这样的

ArrayList<ArrayList<Double>> x = new ArrayList<ArrayList<Double>>();

I have 19 ArrayLists within my x . 我的x内有19个ArrayList。 I have a method called distance(ArrayList x, ArrayList y) which computes the distance between ArrayLists x and y and returns a double . 我有一个名为distance(ArrayList x, ArrayList y) ,该方法计算ArrayLists xy之间的距离并返回double I want to compute the distance between every single vector and all others. 我想计算每个矢量与所有其他矢量之间的距离。

My method would be to do the following 我的方法是执行以下操作

for(int i=0 ; i<(x.size()-1) ; i++)
    {
        for(int j=(i+1) ; j<x.size() ; j++)
        {
            System.out.println(distance( x.get(i) , x.get(j) ));
        }
    }

However, it does not look as efficient. 但是,它看起来不那么有效。 The efficiency is O(N^2). 效率为O(N ^ 2)。 Efficiency is VERY important to me. 效率对我来说非常重要。 I need help to figure out what is the MOST efficient way of doing this operation. 我需要帮助找出执行此操作有效的方法。

If the performance hit is from the algorithm of distance(x,y) and you can't optimize it anyhow, why don't you start by trying to cache the result of distance for a given pair of (x,y)? 如果性能影响是来自distance(x,y)的算法,并且您无法对其进行优化,那么为什么不从尝试缓存给定(x,y)对的距离的结果开始呢? If you do, please consider also the case where distance(x,y) == distance(y,x) . 如果这样做,还请考虑distance(x,y) == distance(y,x)

Moreover, pre-calculate the x.size() and store it might save you some processing. 此外,预先计算x.size()并将其存储可能会节省一些处理时间。

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

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