Java 中的 System.nanoTime() 與 System.currentTimeMillis()
Java 中的時間操作是什麼?
Java 環境提供了兩種時間操作。對於與時間相關的操作,使用者可以使用這些操作。
-
System.nanoTime()
-
System.currentTimeMillis()
System.nanoTime() 主要被稱為昂貴的呼叫,用於獲取更精確的時間值。而 System.currentTimeMillis() 最可靠的已過去時間,有助於根據作業系統獲取時間值。第一個函式以納秒為單位返回時間值(有時可能會返回負值),而第二個函式以毫秒為單位返回時間值。因此很明顯,System.nanoTime()更勝一籌,並且由於其準確性而更受歡迎。
什麼是 System.nanoTime() 和 System.currentTimeMillis()
-
Java 環境中的System.nanoTime() 方法有助於查詢兩個指標之間的差異。
-
簡單來說,它有助於在程序開始之前獲取一次時間讀數,並在執行方法之後獲取另一次時間讀數。
-
不建議每次都對程式碼進行效能分析,因為時間可能會根據系統的作業系統而有所不同。因此,我們可能會遇到一些不準確性。
-
System.currentTimeMillis()是一個執行緒安全的方法,不會返回模稜兩可的結果。
-
此函式將日期、時間和年份作為時間返回。
-
在參考固定的情況下,我們可以獲得準確的輸出。但是,當用戶對系統時間進行一些更改時,它將在那一刻給出錯誤的結果。
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
使用 System.currentTimeMillis() 方法的 Java 程式
System.currentTimeMillis() 方法以毫秒為單位返回當前時間。此方法告訴我們程式碼的一部分執行需要多長時間。
System.currentTimeMillis() 的語法
System.currentTimeMillis(); --> java.language Package --> Declare System Class --> currentTimeMillis() Method
在 Java 中實現 System.currentTimeMillis() 的步驟
-
載入庫。
-
使用TimeMillis() 方法宣告 main() 方法。
-
顯示毫秒、分鐘、小時和天數。
-
列印輸出。
示例 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
此示例演示如何使用System.currentTimeMillis()測量執行迴圈所花費的時間 -
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 環境中的一個高精度時間池,以納秒為單位。此方法始終返回當前值。
System.nanoTime() 的語法
public static long nanoTime() long process startTime = System.nanoTime(); long process estimatedTime = System.nanoTime() - startTime;
在 Java 中實現 System.nano.Time() 的步驟
-
載入庫。
-
使用System.nanoTime()宣告main() 方法。
-
顯示 2 的乘法表。
-
迴圈將生成 2 的倍數(2x 倍數)。
-
列印給定輸入的輸出。
示例 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.nano Time() 方法。對於像高畫質遊戲這樣的快速效能,納秒時間操作是最佳選擇。但為了獲得準確的輸出,我們可以使用System.currentTimeMillis()。
在本文中,我們學習瞭如何使用兩種時間方法獲取時間。我們還使用邏輯構建了一些 Java 程式碼。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP