1

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.

Sandipan Majhi
  • 114
  • 1
  • 10

2 Answers2

2

Your code as shown is correct. Though it would have been convenient to see the start and stop values to verify.

Clock reset

Your computer hardware clock must have corrected or been reset during execution.

This can happen if your host OS is configured to check in with a time server. This configuration is the norm nowadays, as having an internet connection is so common.

The clock could be reset remotely by a sysadmin in some corporate IT scenarios.

System.nanoTime

You can avoid this issue of clock reset by using calls to System.nanoTime if your goal is micro-benchmarking. This command taps into an incrementing count of time since some unspecified moment, often the launch of the JVM or the booting of the host OS. This count of nanoseconds is ever-increasing until reaching the limit of 64-bits (292 years).

This count is not tied to the calendar, does not know about dates or time zones or offset-from-UTC.

Caveat: While ever-increasing the number is not necessarily precise. Today’s conventional clock hardware is not accurate beyond microseconds (if that).

JMH & JEP 230 for benchmarking

If your goal is micro-benchmarking rather than tracking moments in history, see the JMH tool. Now added as a feature to OpenJDK 12 in JEP 230. Discussed in this article.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Okay but somewhere at stack overflow they mentioned that System.nanoTime messes with multithreading cpu's and they suggested System.currentTimeMillis() – Sandipan Majhi Jul 29 '19 at 18:22
  • I have solved the problem. It was stupid of me to set the clock the running before the user inputs. – Sandipan Majhi Jul 29 '19 at 18:32
  • @SandipanMajhi The `System.nanoTime` simply tracks elapsed time but without tying to the calendar’s date and time-of-day. It tracks a count since some unspecified moment, often the launch of the JVM or the launch of the host OS. It is indeed a good way to track elapsed time. I have never heard of the vague issues you mentioned in your comment. Furthermore, `System.currentTimeMillis` is now outmoded by the *java.time* classes, specifically replaced by [`Instant.now`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Instant.html) . – Basil Bourque Jul 30 '19 at 00:30
  • @ Basil Bourque .. Thank you I will be using System.nanoTime ... for time elapsed – Sandipan Majhi Jul 30 '19 at 09:26
0

I edited my program after all. As many suggested I foolishly started timing before the user inputs.

I tried started timing after the user inputs and I finally got 13 milliseconds.

Sandipan Majhi
  • 114
  • 1
  • 10