I have added a code in my java code to calculate the time to finish the program.
final long startTime = System.currentTimeMillis();
and
final long endTime = System.currentTimeMillis();
finally I print the difference between the endtime and starttime.
System.out.println("The total time needed is :" + (endTime - startTime));
But when I run the program the output says
The total time needed is :45194
If the time is in milliseconds that's 45.194 seconds but my program completed in like 3 seconds at max. So help me comprehend this huge number 45194 which is the output.
EDIT :
Here's the whole code
package com.example.TestUnit;
import java.util.Scanner;
public class SurfacePeak {
public static void main(String[] args) {
final long startTime = System.currentTimeMillis();
SurfacePeak s = new SurfacePeak();
int l,m,a,b, peak;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the matrix dimensions :");
a = scan.nextInt();
b = scan.nextInt();
int[][] x = new int [a][b];
System.out.println("Enter the elements of the matrix : ");
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
x[i][j] = scan.nextInt();
}
}
l = 0 ; m = b - 1;
peak = s.GetPeak(x, l, m, a, b);
System.out.println("The peak is : " + peak);
final long endTime = System.currentTimeMillis();
System.out.println("The total time needed is :" + (endTime - startTime));
}
private int GetPeak(int[][] y, int l, int m, int a, int b) {
int midCol = (l+m)/2;
int maxRowindex = GetColMax(y,midCol,a);
if(midCol != 0 && midCol != b-1) {
if(y[maxRowindex][midCol - 1] > y[maxRowindex][midCol]) {
m = midCol - 1;
return GetPeak(y,l,m,a,b);
}else if(y[maxRowindex][midCol + 1] > y[maxRowindex][midCol]) {
l = midCol + 1;
return GetPeak(y,l,m,a,b);
}
}else if(a==2 && b == 2 && midCol == 0 || midCol == b-1) {
if(y[maxRowindex][midCol+1] > y[maxRowindex][midCol] && midCol == 0 )
return y[maxRowindex][midCol+1];
else if(midCol == b -1 && y[maxRowindex][midCol-1]>y[maxRowindex][midCol])
return y[maxRowindex][midCol-1];
}
return y[maxRowindex][midCol];
}
private int GetColMax(int[][] a, int mid, int row) {
int max = a[0][mid], maxRow = 0;
for(int i=0; i<row; i++) {
if(a[i][mid] >= max) {
max = a[i][mid];
maxRow = i;
}
}
return maxRow;
}
Here's the output also
Enter the matrix dimensions :
4 4
Enter the elements of the matrix :
10 8 10 10 14 56 78 12 90 99 24 300 6 8 1 2
The peak is : 99
The total time needed is :25235
The last time I ran it showed 25235 ... which is much more compared to the actual time.
EDIT: It was so stupid of me to start timing before the user inputs which was the main problem here.