Java System.nanoTime() 与 System.currentTimeMillis

javaobject oriented programmingprogramming更新于 2024/3/15 14:04:17

Java 中的时间操作有哪些?

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

  • System.nanoTime()

  • System.currentTimeMillis()

System.nanoTime() 主要被称为昂贵调用,用于获取更具体的时间值。而且,System.currentTimeMillis() 是最真实的可能传递时间,有助于根据操作系统获取时间值。第一个函数以纳秒为单位返回时间值(有时会给出负值),而第二个函数以毫秒为单位返回时间值。因此很明显,System.nanoTime() 领先一步,其准确性更容易被接受。

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

  • Java 环境中的 System.nanoTime() 方法有助于找出两个指针之间的差异。

  • 简而言之,它有助于在进程启动前获取一个时间读数,并在执行该方法后获取另一个时间读数。

  • 不建议每次都对代码进行分析,因为时间可能因系统的操作系统而异。因此,我们可能会遇到一些不准确的情况。

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

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

  • 只要参考固定,我们就能获得准确的输出。但是当用户对系统时间进行一些更改时,它会在那一刻给出错误的结果。

System.nano.Time() 的算法

  • 步骤 1 - 加载库。

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

  • 步骤 3 - 显示 2 的表格。

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

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

System.currentTimeMillis() 的算法

  • 步骤 1 - 加载库。

  • 步骤 2 - 声明主方法通过使用 TimeMillis() 方法。

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

  • 步骤 4 − 打印输出。

System.currenyTimeMillis() 的语法

System.currentTimeMillis();
--> java.language 包
--> 声明系统类
--> currentTimeMillis() 方法

System.nanoTime() 的语法

public static long nanoTime()
long process startTime = System.nanoTime();
long process perceivedTime = 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

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

System nanoTime() 方法是一个 Java 环境,它是一个以纳秒为单位的高清时间池。此方法始终返回当前值。

示例 1

使用 nano Time() 差异,我们可以计算空循环运行接下来 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.nano Time() 方法。对于像高清游戏这样的快速性能,纳米时间操作是最好的选择。但为了获得准确的输出,我们可以使用 System.currentTimeMillis()。

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


相关文章


有用资源