実験課題1

Exercise 1(課題1) See the section 6.1. "About Profiler in NetBeans" and 6.2.1. "Analysis of CPU Performance". Show which one of two programs as described below are slower and explain why the program is slow. Follow instructions below to show the result.

  1. create the Java application project named chapter6kadai1.
  2. create main and each sort method as described below in the main class in the project.
  3. take snapshots in the profiler by switching each sort method in main method.
    • If the profiler does not show the dialog of Figure 11, must increase size parameter more than 10000 described in the 5th line of each below main method.
  4. show difference of execution time by comparing snapshots.
public static void sortA(double[] l) {
    
    for (int j = 0; j < l.length; j = j + 1) {

        for (int i = 0; i < l.length - j - 1; i = i + 1) {
            
            if (l[i] > l[i+1]) {
                
                double d = l[i];
                l[i] = l[i+1];
                l[i+1] = d;
            }
        }
    }
}

public static void sortB(Double[] l) {
    
    for (int j = 0; j < l.length; j = j + 1) {
        
        for (int i = 0; i < l.length - j - 1; i = i + 1) {
            
            if (l[i] > l[i+1]) {
                
                Double d = l[i];
                l[i] = l[i+1];
                l[i+1] = d;
            }
        }
    }
}
Figure:Two sort class methods that are different in program text but the same in algorithm
public static void main(String[] args) {

    Random rng = new Random();
    
    int size = 10000;
    
    double[] rdl = new double[size];
    
    for (int i = 0; i < size ; i++) {
        
        rdl[i] = rng.nextDouble() * size;
    }
    
    sortA(rdl);
}
Figure:`main` method for `sortA` class method
public static void main(String[] args) {

    Random rng = new Random();
    
    int size = 10000;
    
    Double[] vdl = new Double[size];
    
    for (int i = 0; i < size ; i++) {
        
        vdl[i] = rng.nextDouble() * size;
    }
    
    sortB(vdl);
}
Figure:`main` method for `sortB` class method

(6.1.節および6.2.1.節をよく読み、chapter6kadai1プロジェクトを作成し、chapter6kadai1クラスに以下に示すアルゴリズムが同一のsortAクラスメソッド、sortBクラスメソッドを追加しなさい。その後、mainメソッドをプロファイラ実行のたびに使い分けることで、それぞれのスナップショットを作成し、どちらのプログラムの方が遅いか、差分表示にて図21のように示しなさい。また、なぜそのプログラムが遅いのか考察しなさい。 なお、図11のようにスナップショットを取得するかどうかのダイアログが表示されない場合は、mainメソッドの5行目のsizeの値を増やして表示されるように調整して、課題を実施してください。 )