java - For Loop only printing/storing last iteration -
hope can make myself clear enough.
i'm trying measure how long take program multiply couple of matrices , storing result 1 (called c).
i need record time going 1 100, 1 200, 1 300, etc.
i'm measuring time , storing result .dat file ploting later on (using gnuplot)
thing is... when run it, last iteration n times wanted measure. let's i'm trying 1 100, 100 iterations, in .dat file, last iteration. i'm sorry if i'm not making myself clear.
here's code , can understand. thank you!!
import java.util.timer; import java.util.scanner; public class matrixmult { public static void main(string[] args) { //creating new scanner input stdin// scanner input = new scanner(system.in); //creates variable store nxn dimension of matrices// int ms = input.nextint(); //initializing matrices , giving them dimensions provided stdin// int [] [] = new int [ms] [ms]; int [] [] b = new int [ms] [ms]; int [] [] c = new int [ms] [ms]; //populating matrices 1's , 0's// populatingmatrices (a); populatingmatrices (b); //closing scanner after being used// input.close(); long t_start; long t_end; long t_cost = 00000000000l; (int x = 0; x < ms ;x++ ) { //starts timer// t_start = system.nanotime(); //storing result of matrices multiplication c// c = mmm(a,b,c); //end timer t_end = system.nanotime(); t_cost = t_end - t_start; system.out.println(); system.out.println((t_cost / 1000000.0)); } }//main method// //method populate matrices 1's , 0's// public static int [][] populatingmatrices(int [] []) { (int = 0; < a.length; i++) { (int j = 0; j < a[0].length; j++) { if (i == j) { a[i][j] = 1; } else { a[i][j] = 0; } } //returns matrix populated 1's accross diagonal axis, , 0's in other elements of matrix// }return a; } //method multiply identity matrices// public static int [][] mmm (int [][], int b [][], int c [][]) { int nr; int nc; nr = nc = a.length; (int = 0; < nr; i++) { (int j = 0; j < nr ; j++ ) { (int k = 0;k < nr ;k++) { c[i][j] = c[i][j] + a[i][j] * b[k][j]; } } } return c; } }
thank much!
you getting last time of recent iteration because continuously overwriting value of t_cost. need += add each iteration of loop.
long t_start; long t_end; long t_cost = 00000000000l; (int x = 0; x < ms ;x++ ) { //starts timer// t_start = system.nanotime(); //storing result of matrices multiplication c// c = mmm(a,b,c); //end timer t_end = system.nanotime(); //use += adding previous iterations of calcualted time t_cost += t_end - t_start; system.out.println(); system.out.println((t_cost / 1000000.0)); }
Comments
Post a Comment