希望我可以讓自己清楚。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;
}
}
非常感謝!
單個字母的變量名稱和零註釋使這個不可讀。 – DejaVuSansMono
如何編寫_.dat_文件? – Berger
@DejaVuSansMono所以不要打擾,甚至只是回覆說,哥們。你根本沒有幫助。 – Juancho