计算Java程序的执行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21964882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:42:21  来源:igfitidea点击:

Calculate Execution Time of Java Program

java

提问by Kakarot

I want to compute the execution time of my Java Program and I am confused how I should go about it. I know that time elapsed can be computed using System.nanoTime() & System.currentTimeInMillis(), however this approach does not take into consideration the underlying scheduling mechanism of the Operating System.

我想计算我的 Java 程序的执行时间,我很困惑我应该如何去做。我知道可以使用 System.nanoTime() 和 System.currentTimeInMillis() 计算经过的时间,但是这种方法没有考虑操作系统的底层调度机制。

For example : Say there are 3 processes running at the same time (lets assume its a single core system)

例如:假设有 3 个进程同时运行(假设它是一个单核系统)

Process A

流程A

Process B (My Java Code - running in JVM Process)

进程 B(我的 Java 代码 - 在 JVM 进程中运行)

Process C

工艺C

Now if Process B starts executing at 11:00 AM and completes execution at 11:02 AM, System.nanoTime() & System.currentTimeInMillis() will report the time taken as 2 minutes. However its possible that Process B might not be running for the duration of entire 2 minutes, because the OS will schedule the other 2 Process and they would be running for some amount of time in the 2 minute interval. So in essence the java Program would run for less than 2 minutes.

现在,如果进程 B 在上午 11:00 开始执行并在上午 11:02 完成执行,System.nanoTime() 和 System.currentTimeInMillis() 将报告花费的时间为 2 分钟。但是,进程 B 可能不会在整个 2 分钟内运行,因为操作系统将调度其他 2 个进程,并且它们将在 2 分钟的时间间隔内运行一段时间。所以本质上java程序将运行不到2分钟。

Is there some way to pinpoint the exact time that was taken by a Java Program itself to complete execution? Note :

有什么方法可以查明 Java 程序本身完成执行所用的确切时间?笔记 :

  1. We can run the code multiple times and take average time to minimize the effects of such cases, still it would be good to know if there is some reliable way to compute the execution time

  2. Lets assume that the portion of code that we need to time cannot be broken into smaller chunks and takes a large amount of time to process.

  1. 我们可以多次运行代码并取平均时间来最小化这种情况的影响,但最好知道是否有一些可靠的方法来计算执行时间

  2. 让我们假设我们需要计时的代码部分不能被分解成更小的块并且需要大量的时间来处理。

回答by KRUKUSA

The old fashion way is... Break it into methods

旧的时尚方式是......把它分解成方法

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = endTime - startTime;

回答by akhil_mittal

Just in case you are using Java 8 then it has Instantclass which represents an instantaneous point on the time-line. This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application as:

以防万一您使用的是 Java 8,那么它具有Instant表示时间线上瞬时点的类。此类对时间线上的单个瞬时点进行建模。这可用于将应用程序中的事件时间戳记为:

    Instant start = Instant.now();
    try {
        Thread.sleep(7000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Instant end = Instant.now();
    System.out.println(Duration.between(start, end));
}

And it prints: PT7.001S

它打印: PT7.001S