System.currentTimeMillis() 方法:返回巨大的执行时间值然后实际执行时间 [英] System.currentTimeMillis() method: returning huge value for execution time then actual execution time

查看:39
本文介绍了System.currentTimeMillis() 方法:返回巨大的执行时间值然后实际执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的java代码中添加了一个代码来计算完成程序的时间.

I have added a code in my java code to calculate the time to finish the program.

final long startTime = System.currentTimeMillis();

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

如果时间以毫秒为单位,那就是 45.194 秒,但我的程序最多在 3 秒内完成.所以帮我理解这个巨大的数字 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.

这是完整的代码

 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;

    }

这里的输出也是

    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

我上次运行时显示 25235 ...与实际时间相比要多得多.

The last time I ran it showed 25235 ... which is much more compared to the actual time.

在用户输入之前开始计时真是太愚蠢了,这是这里的主要问题.

It was so stupid of me to start timing before the user inputs which was the main problem here.

推荐答案

您显示的代码是正确的.尽管查看要验证的开始和停止值会很方便.

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

您的计算机硬件时钟在执行期间必须已更正或已重置.

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.

在某些企业 IT 场景中,系统管理员可以远程重置时钟.

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

您可以通过调用 System.nanoTime 如果您的目标是 微基准测试.此命令利用自某个未指定时刻开始递增的时间计数,通常是 JVM 启动或主机操作系统启动.这个纳秒计数不断增加,直到达到 64 位(292 年)的限制.

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).

此计数与日历相关联,不知道日期或时区或UTC偏移量.

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 工具.现在作为一项功能添加到 OpenJDK 12 中的 JEP 230.在本文中讨论.

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.

这篇关于System.currentTimeMillis() 方法:返回巨大的执行时间值然后实际执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆