Javaで経過/実行時間を計算する方法は?
一般に、経過時間または実行は、イベントの開始点から終了点までの時間です。以下は、Javaで経過時間を見つけるためのさまざまな方法です-
- currentTimeMillis()メソッドは、現在の時刻をミリ秒単位で返します。メソッドの経過時間を見つけるには、目的のメソッドの実行前後の時間値の差を取得できます。
- nanoTime()メソッドは、現在の時刻をナノ秒単位で返します。メソッドの経過時間を見つけるには、目的のメソッドの実行前後の時間値の差を取得できます。
- Instantクラスのnow()メソッドは現在の時刻を返し、Duration.between()メソッドは、これらを使用してメソッドの実行にかかる時間を見つけることができる、指定された2つの時間値の差を返します。
- Apache Commonsライブラリは、Stopwatchと呼ばれるクラスを提供し、start()stop()メソッドとgetTime()メソッドを提供し、これらを使用してメソッドの実行にかかる時間を見つけることができます。
- java.util.DateクラスのgetTime()メソッドは、現在の時刻を返します。メソッドの経過時間を見つけるには、目的のメソッドの実行前後の時間値の差を取得できます。
- getInstance()。getTime()。getTime()を使用して、現在のインスタンスの時間を取得することもできます。 差を取得して経過時間を見つけた後。
例:currentTimeMillis()メソッドの使用
public class Example {
public void test(){
int num = 0;
for(int i=0; i<=50; i++){
num =num+i;
System.out.print(num+", ");
}
}
public static void main(String args[]){
//Start time
long begin = System.currentTimeMillis();
//Starting the watch
new Example().test();
//End time
long end = System.currentTimeMillis();
long time = end-begin;
System.out.println();
System.out.println("Elapsed Time: "+time +" milli seconds");
}
} 出力
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 4 milli seconds
例:nanoTime()メソッドの使用
public class Example {
public void test(){
int num = 0;
for(int i=0; i<=50; i++){
num =num+i;
System.out.print(num+", ");
}
}
public static void main(String args[]){
//Start time
long begin = System.nanoTime();
//Starting the watch
new Example().test();
//End time
long end = System.nanoTime();
long time = end-begin;
System.out.println();
System.out.println("Elapsed Time: "+time);
}
} 出力
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 1530200
例:Instantクラスの使用
import java.time.Duration;
import java.time.Instant;
public class Example {
public void test(){
int num = 0;
for(int i=0; i<=50; i++){
num =num+i;
System.out.print(num+", ");
}
}
public static void main(String args[]) {
//Starting time
Instant start = Instant.now();
new Example().test();
//End time
Instant end = Instant.now();
long time = Duration.between(start, end).toMillis();
System.out.println();
System.out.println(time+" Milli seconds");
}
} 出力
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 3 Milli seconds
例:StopWatchクラスの使用
import org.apache.commons.lang3.time.StopWatch;
public class Example {
public void test(){
int num = 0;
for(int i=0; i<=50; i++){
num =num+i;
System.out.print(num+", ");
}
}
public static void main(String args[]) {
//Instantiating the StopWatch class
StopWatch obj = new StopWatch();
//Starting the watch
obj.start();
new Example().test();
//Stopping the watch
obj.stop();
System.out.println();
System.out.println("Elapsed Time: "+obj.getTime() +" milli seconds");
}
} 出力
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 1 milli seconds
例:getTime()メソッドの使用
import java.util.Date;
public class Demo{
public void test(){
int num = 0;
for(int i=0; i<=50; i++){
num =num+i;
System.out.print(num+", ");
}
}
public static void main(String[] args) {
long startTime = new Date().getTime();
new Demo().test();
long endTime = new Date().getTime();
long time = endTime - startTime;
System.out.println("Execution time: " + time);
}
} 出力
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Execution time: 5
例:Calendarクラスの使用
import java.util.Calendar;
public class Demo{
public void test(){
int num = 0;
for(int i=0; i<=50; i++){
num =num+i;
System.out.print(num+", ");
}
}
public static void main(String[] args) throws InterruptedException {
long startTime = Calendar.getInstance().getTime().getTime();
new Demo().test();
long endTime = Calendar.getInstance().getTime().getTime();
long time = endTime - startTime;
System.out.println("Execution time: " + time);
}
} 出力
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Execution time: 20
-
JavaでinvokeLater()メソッドを呼び出すにはどうすればよいですか?
invokeLater() メソッドは静的です SwingUtilitiesのメソッド クラスであり、タスクを非同期に実行するために使用できます。 AWT イベントディスパッチャスレッド 。 SwingUtilities.invokeLater() メソッドはSwingUtilities.invokeAndWait()のように機能します ただし、リクエストはイベントキューに配置されます そしてすぐに戻る 。 invokeLater() メソッドは、実行可能内のコードのブロックを待機しません ターゲットによって参照されます 実行します。 構文 public static void in
-
Excelで時間差を計算する方法
Microsoft Excel 予算計算、税計算、およびその他の多くの目的に使用できます。それが私たちに提供する機能のために、あなたはそれを別の形の計算機として使うことができます。 Excelを使用して、2つの時間の差を計算することもできます。これは、特定の開始時間と終了時間の間に従業員が働いた時間数を計算するのに役立ちます。従業員の労働時間の全記録は、Excelを使用して維持できます。これは、従業員に支払われる賃金または給与の計算に役立ちます。 開始時刻から終了時刻を引くだけなので、時間差を計算するのはとても簡単だと思いますか?先に進んで同じことをすると、2つの問題に直面します。そ