为什么这个循环过程需要这么长时间?

时间:2014-08-26 16:32:31

标签: java performance loops arraylist

我正在编写Java代码,将过滤器应用于图像(由数组(300 x 300值)组成)。我从数组中获取了许多相邻值,对它们求平均值并将它们添加到ArrayList中,以生成新值,最终生成过滤后的图像。我很欣赏ArrayList是巨大的(87616个对象),但我不知道如何更好地打破这个过程。

如你所见,我是新手。感谢所有的帮助!

import java.util.ArrayList;

public class Analyst {


    /**
    * Creates a storage object of class Storage within which array data is held
    */
    public Analyst () {
        Storage store;
        store = new Storage();

        //Create new DataReader class, in which original data can be read from, initialise it with object file
        DataReader file = new DataReader();

        //Create neighbours ArrayList to hold neighbour objects within Moore radius
        ArrayList<Double> neighbours = new ArrayList<Double>();

        //Create newValues ArrayList to hold mean value calculated from neighbours
        ArrayList<Double> newValues = new ArrayList<Double>();

        //Create loop to run sequence for subsequent lines
        for (int y = 0; y < 296; y++){

            //Create loop to run average and newValue methods for first line (note that the first value averaged is in [2][2], last is [2][297]
            for (int x = 0; x < store.image.length - 4; x++){

                // Establish size of Moore Radius = 2r+1
                // Run loop to extract first 25 values to new neighbours array
                for (int i = 0 + x; i < 5 + x; i++) {
                    for (int j = 0 + y; j < 5 + y; j++){
                        neighbours.add(store.image[i][j]);
                        //System.out.println("Normal store values " + store.image[i][j]);
                    }
                }

                //Instantiate new variable to hold total value of objects in neighbour array list
                double total = 0;
                for (int k = 0; k < neighbours.size(); k++){
                    total += neighbours.get(k);
                }

                //Instantiate new variable to hold Mean Filter Moor neighbourhood value
                double neighbourhoodAverage = (total / neighbours.size());
                //System.out.println("Total = " + total);
                //System.out.println("Average = " + neighbourhoodAverage);

                //add new average value to newValues ArrayList
                newValues.add(neighbourhoodAverage);

            }
        }
        //Print to check correct number of values in row have been added to newValues ArrayList
        System.out.println("newValues ArrayList is made up of " + newValues.size() + " averaged values");

    }
    public static void main (String args[]) {
        new Analyst();
    }
}

2 个答案:

答案 0 :(得分:1)

这里有3个循环。每个约300个操作。那是O(n ^ 3),其中约有2700万次操作!

答案 1 :(得分:1)

你永远不会清除邻居列表。因此,在处理图像的最后一个像素时,它将包含大约25 * 300 * 300个元素。这比正确的天真实施大约多90000倍。

顺便说一句,我根本不知道为什么你需要这个中介名单。为什么不简单地做:

            double total = 0;
            for (int i = 0 + x; i < 5 + x; i++) {
                for (int j = 0 + y; j < 5 + y; j++){
                    total += store.image[i][j];
                    //System.out.println("Normal store values " + store.image[i][j]);
                }
            }
            double average = total / 25;