【问题标题】:Why does this looping process take so long?为什么这个循环过程需要这么长时间?
【发布时间】:2014-08-26 16:32:31
【问题描述】:

我正在编写 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();
    }
}

【问题讨论】:

  • 你说它需要“很长时间”。那是多长时间? (我们在这里说的是毫秒还是小时......?)
  • 大约需要 15 分钟。

标签: java performance loops arraylist


【解决方案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;

【讨论】:

  • 一个简洁而正确的回答,我是新手,很难发现所有这些东西。我已经添加了这行代码,它在一瞬间完成。祝你有美好的一天!
【解决方案2】:

这里有 3 个循环。每个大约 300 次操作。那是 O(n^3),对于 300 次,它大约有 27000000 次操作!

【讨论】:

  • 如何或在哪里将其分解为单独的循环?
  • @meriton 没关系,它有 (1+2+3+...300) 那是 n(n+1)/2 ,对于里面的 2 个循环来说是 O(n^2)
  • @AndrewLyons 这更像是一个算法问题,而不是编程问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 2011-12-07
  • 2012-08-09
  • 2021-07-26
  • 1970-01-01
相关资源
最近更新 更多