2016-11-17 49 views
-3

希望我可以讓自己清楚。For Loop僅打印/存儲最後一次迭代

我試圖測量這個程序需要多長時間乘以幾個矩陣並將結果存儲到另一個矩陣(調用C)中。

我需要記錄的時間會從1到100,1 200,1 300,等等。

我實際測量的時間和結果存儲到一個.dat文件ploting以後(使用gnuplot)

事情是......當我運行它時,我只得到我想測量的最後一次迭代N次。假設我從1到100嘗試,我得到100次迭代,但在.dat文件中,我只得到最後一次迭代。如果我沒有說清楚,我很抱歉。

這是代碼,希望你能理解。謝謝!!

import java.util.Timer; 
import java.util.Scanner; 

public class matrixMult 
{ 
    public static void main(String[] args) 
    { 
     //Creating a new Scanner to get input from Stdin// 
     Scanner input = new Scanner(System.in); 

     //Creates a variable to store the NxN dimension of the Matrices// 
     int ms = input.nextInt(); 

     //Initializing Matrices and giving them the dimensions provided by the Stdin// 
     int [] [] A = new int [ms] [ms]; 
     int [] [] B = new int [ms] [ms]; 
     int [] [] C = new int [ms] [ms]; 

     //Populating the Matrices with 1's and 0's// 
     populatingMatrices (A); 
     populatingMatrices (B); 

     //Closing the Scanner after being used// 
     input.close(); 

     long t_start; 
     long t_end; 
     long t_cost = 00000000000L; 

     for (int x = 0; x < ms ;x++) 
     { 
      //Starts the timer// 
      t_start = System.nanoTime(); 

      //Storing the result of the matrices multiplication into C// 
      C = mmm(A,B,C); 


      //End the timer 
      t_end = System.nanoTime(); 

      t_cost = t_end - t_start; 

      System.out.println(); 
      System.out.println((t_cost/1000000.0)); 
     } 

    }//Main Method// 

    //Method to populate Matrices with 1's and 0's// 
    public static int [][] populatingMatrices(int A [] []) 
    { 
     for (int i = 0; i < A.length; i++) 
     { 
      for (int j = 0; j < A[0].length; j++) 
      { 
       if (i == j) 
       { 
        A[i][j] = 1; 
       } 
       else 
       { 
        A[i][j] = 0; 
       } 
      } 
     //Returns Matrix populated with 1's accross the diagonal axis, and 0's in all other elements of the Matrix// 
     }return A; 
    } 


    //Method to multiply Identity Matrices// 
    public static int [][] mmm (int a [][], int b [][], int c [][]) 
    { 
     int nr; 
     int nc; 
     nr = nc = a.length; 

     for (int i = 0; i < nr; i++) 
     { 
      for (int j = 0; j < nr ; j++) 
      { 
       for (int k = 0;k < nr ;k++) 
       { 
        c[i][j] = c[i][j] + a[i][j] * b[k][j];  
       } 
      } 
     } 
     return c; 
    } 
} 

非常感謝!

+0

單個字母的變量名稱和零註釋使這個不可讀。 – DejaVuSansMono

+0

如何編寫_.dat_文件? – Berger

+0

@DejaVuSansMono所以不要打擾,甚至只是回覆說,哥們。你根本沒有幫助。 – Juancho

回答

0

由於您不斷覆蓋t_cost的值,您只能獲得最近一次迭代的最後一次。你需要做一個+ =來爲你的for循環迭代添加它。

long t_start; 
    long t_end; 
    long t_cost = 00000000000L; 

    for (int x = 0; x < ms ;x++) 
    { 
     //Starts the timer// 
     t_start = System.nanoTime(); 

     //Storing the result of the matrices multiplication into C// 
     C = mmm(A,B,C); 


     //End the timer 
     t_end = System.nanoTime(); 
     //use the += so you are adding to the previous iterations of your calcualted time 
     t_cost += t_end - t_start; 

     System.out.println(); 
     System.out.println((t_cost/1000000.0)); 
    } 
+0

我試過你的解決方案,但它繪製了一條直線,不應該如此。不管怎麼說,多謝拉。 – Juancho

0

我認爲for循環中的「ms」應該是您希望for循環運行的次數。我看不出爲什麼它與您在上面的代碼中設置的數組大小相關聯。

 ArrayList<long> al = new ArrayList() 
     al.add(System.nanoTime());//Start time; 
     for (int x = 0; x < ms ;x++) 
     { 
      //Storing the result of the matrices multiplication into C// 
      C = mmm(A,B,C); 

      if(x == 100 || x == 200) 
      { 
       al.add(System.nanoTime());//at given points time 
      } 
     } 
     al.add(System.nanoTime());//end time. if You end at a point that you check in the if statment like 200, then this is not needed. 

     //now do your cost calculations using all the values stored in al.