सामान्य तौर पर, बीता हुआ समय या निष्पादन समय किसी घटना के प्रारंभ बिंदु से अंत बिंदु तक का समय होता है। जावा में बीता हुआ समय ज्ञात करने के विभिन्न तरीके हैं:
currentTimeMillis()मेथड वर्तमान समय को मिलीसेकंड में लौटाता है। वांछित मेथड के निष्पादन से पहले और बाद के समय मानों के बीच का अंतर लेकर बीता हुआ समय ज्ञात करें।nanoTime()मेथड वर्तमान समय को नैनोसेकंड में लौटाता है। निष्पादन से पहले और बाद के समय मानों के बीच का अंतर लेकर बीता हुआ समय ज्ञात करें।Instantक्लास काnow()मेथड वर्तमान समय लौटाता है औरDuration.between()मेथड दिए गए दो समय मानों के बीच का अंतर लौटाता है। इनका उपयोग करके मेथड निष्पादन समय ज्ञात करें।- 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[]){
// प्रारंभ समय
long begin = System.currentTimeMillis();
// मेथड निष्पादित करना
new Example().test();
// अंत समय
long end = System.currentTimeMillis();
long time = end-begin;
System.out.println();
System.out.println("बीता हुआ समय: "+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, बीता हुआ समय: 4 मिली सेकंड
उदाहरण: 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[]){
// प्रारंभ समय
long begin = System.nanoTime();
// मेथड निष्पादित करना
new Example().test();
// अंत समय
long end = System.nanoTime();
long time = end-begin;
System.out.println();
System.out.println("बीता हुआ समय: "+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, बीता हुआ समय: 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[]) {
// प्रारंभ समय
Instant start = Instant.now();
new Example().test();
// अंत समय
Instant end = Instant.now();
long time = Duration.between(start, end).toMillis();
System.out.println();
System.out.println(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, 3 मिली सेकंड
उदाहरण: 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[]) {
// StopWatch क्लास को इंस्टेंटिएट करना
StopWatch obj = new StopWatch();
// घड़ी शुरू करना
obj.start();
new Example().test();
// घड़ी रोकना
obj.stop();
System.out.println();
System.out.println("बीता हुआ समय: "+obj.getTime() +" मिली सेकंड");
}
}
आउटपुट
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, बीता हुआ समय: 1 मिली सेकंड
उदाहरण: 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("निष्पादन समय: " + 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, निष्पादन समय: 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("निष्पादन समय: " + 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, निष्पादन समय: 20