Java System.nanoTime() vs System.currentTimeMillis

Java System.nanoTime() vs System.currentTimeMillis

Java中的时间操作是什么

Java环境中提供了两种时间操作。对于与时间有关的操作,用户可以使用这些操作。

  • System.nanoTime()

  • System.currentTimeMillis()

System.nanoTime()主要被称为昂贵的调用,用于获取更具体的时间值。而System.currentTimeMillis()是最真实可能经过的时间,可以根据操作系统基本上获取时间值。第一个函数返回纳秒级的时间值(有时可能为负值),而另一个函数返回毫秒级的时间值。所以很明显,System.nanoTime()更加准确,更可接受。

System.nanoTime()和System.currentTimeMillis()是什么

  • System.nanoTime()方法在Java环境中可以帮助找到两个指针之间的差异。

  • 简单来说,它可以在进程开始前获取一个时间读数,然后在执行方法后获取另一个时间读数。

  • 并不建议每次都对代码进行分析,因为时间可能会根据系统的操作系统而有所不同。因此,我们可能会遇到一些不准确性。

  • System.currentTimeMillis()是一个线程安全的方法,不会返回模棱两可的结果。

  • 该函数返回日期、时间和年份作为时间。

  • 在引用固定的情况下,我们可以获得准确的输出。但是当用户更改系统时间时,它会在那一刻给出错误的结果。

System.nanoTime()算法

  • 步骤 1 – 加载库。

  • 步骤 2 – 使用System.nanoTime()声明主要方法。

  • 步骤 3 – 显示2的乘法表。

  • 步骤 4 – 循环将产生2的倍数(2倍)。

  • 步骤 5 – 从给定的输入打印输出。

System.currentTimeMillis()算法

  • 步骤 1 – 加载库。

  • 步骤 2 – 使用TimeMillis()方法声明主要方法。

  • 步骤 3 – 显示毫秒数、分钟数、小时数和天数。

  • 步骤 4 – 打印输出。

System.currenyTimeMillis()的语法

System.currentTimeMillis();
--> java.language Package
--> Declare System Class
--> currentTimeMillis() Method

System.nanoTime()的语法

public static long nanoTime() 
long process startTime = System.nanoTime(); 
long process estimatedTime = System.nanoTime() - startTime;

System.nanoTime()和System.currentTimeMillis()的比较

下面是两个特定的Java时间操作的通用示例-

package javalangtimeslot;
import java.lang.*;
public class javatimeslot {

   public static void main(String[] args) { 
      System.out.print("Time in nanoseconds we can see here = ");
      System.out.println(System.nanoTime());
      System.out.print("Time in milliseconds we can see here = "); 
      System.out.println(System.currentTimeMillis());
   }
}

输出

Time in nanoseconds we can see here = 4083437933186033
Time in milliseconds we can see here = 1680778649732

不同的方法遵循

  • 方法1 – 使用System.currentTimeMillis()方法的Java程序

  • 方法2 – 使用Java System.nanoTime()的Java程序

使用System.currentTimeMillis()方法的Java程序

System.currentTimeMillis()方法以毫秒为单位返回当前时间。这个方法计算一段代码执行所花费的时间。

示例1

import java.io.*;
public class timefuncjava {
    public static void main(String[] args){
        System.out.println("Milliseconds Are Here : " + System.currentTimeMillis());
        System.out.println("Seconds Are Here: " + (System.currentTimeMillis())/ 2000);
        System.out.println("Minutes Are Here: " + (System.currentTimeMillis())/ 2000 / 120);    
        System.out.println("Hours Are Here: " + (System.currentTimeMillis())/ 2000 / 120 / 120);
        System.out.println("Days Are: " + (System.currentTimeMillis())/ 2000 / 120 / 120 / 48); 
        System.out.println("Years Total: " + (System.currentTimeMillis()) / 2000 / 120 / 120 / 48 / 365);
        System.out.println("Milliseconds Are Here : " + System.currentTimeMillis());
        System.out.println("Seconds Are Here: " + (System.currentTimeMillis())/ 2000);
        System.out.println("Minutes Are Here: " + (System.currentTimeMillis())/ 2000 / 120);    
        System.out.println("Hours Are Here: " + (System.currentTimeMillis())/ 2000 / 120 / 120);
        System.out.println("Days Are: " + (System.currentTimeMillis())/ 2000 / 120 / 120 / 48); 
        System.out.println("Years Total: " + (System.currentTimeMillis()) / 2000 / 120 / 120 / 48 / 365);

    }
}

输出

Milliseconds Are Here : 1680778875055
Seconds Are Here: 840389437
Minutes Are Here: 7003245
Hours Are Here: 58360
Days Are: 1215
Years Total: 3
Milliseconds Are Here : 1680778875062
Seconds Are Here: 840389437
Minutes Are Here: 7003245
Hours Are Here: 58360
Days Are: 1215
Years Total: 3

在此构建代码中,我们使用TimeMillis来获取毫秒、秒、分钟、小时、天和年份。

示例2

public class timeprogjava{
   public static void main(String args[]){
      long starting, ending;
      System.out.println("Timing a loop from 0 to 100,000,000,000");
      starting= System.currentTimeMillis(); 
      for(long k=0;k<100000000000L;k++);
      ending=System.currentTimeMillis(); 
      System.out.println("Elapsed time Is: "+(ending-starting));
   }
}

输出

Timing a loop from 0 to 100,000,000,000

Java程序使用System.nanoTime()方法

System.nanoTime()方法是Java环境中的高精度时间池,以纳秒为单位。该方法始终返回当前值。

示例1

使用nano Time() -difference,我们可以计算空循环运行下一个1000次所消耗的时间。我们将在循环之前和之后获取时间输出。

public class javacodetime {
   public static void main(String args[]) {
      long nanoTime2 = System.nanoTime();
      for(int k=0; k < 10000; k++) {

      }
      long nanoTime5 = System.nanoTime();    
      long runTimeInNanoSeconds = nanoTime5 - nanoTime2;    

      System.out.println("Time taken to execute the process by for loop : " + runTimeInNanoSeconds + " nano seconds");   
   }  
}

输出

Time taken to execute the process by for loop : 132509 nano seconds

示例2

此示例将通过使用System.nanoTime()方法以纳秒为单位获取当前时间。

public class javatimecode {
   public static void main(String args[]) {
      long nanoTime = System.nanoTime();       
      System.out.println("Current time as in Nano Seconds : "+nanoTime);

   }  

}

输出

Current time as in Nano Seconds : 4084089973012410

结论

总结来说,当问题过于繁重时,需要使用System.nanoTime()方法。对于像高清游戏等性能要求较高的情况,纳秒级时间操作是最佳选择。但是,为了获得准确的输出,我们可以使用System.currentTimeMillis()。

在本文中,我们学习了如何使用这两种时间方法来获取时间。我们还使用了逻辑构建了一些Java代码。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程