सामान्य तौर पर, बीता हुआ समय किसी घटना के शुरुआती बिंदु से समाप्ति बिंदु तक का समय होता है। जावा में बीता हुआ समय खोजने के विभिन्न तरीके निम्नलिखित हैं -
currentTimeMillis() विधि का उपयोग करना
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
नैनोटाइम () पद्धति का उपयोग करना
नैनोटाइम () विधि नैनो सेकंड में वर्तमान समय लौटाती है। किसी विधि के लिए बीता हुआ समय खोजने के लिए आप वांछित विधि के निष्पादन से पहले और बाद के समय मानों के बीच अंतर प्राप्त कर सकते हैं।
उदाहरण
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
तत्काल कक्षा का उपयोग करना
इंस्टेंट क्लास की अब () विधि वर्तमान समय लौटाती है और अवधि। बीच () विधियाँ दिए गए दो समय मानों के बीच का अंतर लौटाती हैं ताकि बीता हुआ समय वांछित विधि के निष्पादन से पहले और बाद के समय के मूल्यों को पुनः प्राप्त कर सके और पुनः प्राप्त कर सके। Duration.between() विधि का उपयोग करने की अवधि।
उदाहरण
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
स्टॉपवॉच क्लास का उपयोग करना
अपाचे कॉमन्स लाइब्रेरी स्टॉपवॉच के रूप में जाना जाने वाला एक वर्ग प्रदान करती है, यह एक विधि के निष्पादन के लिए लगने वाले समय को खोजने के लिए स्टार्ट () स्टॉप () और गेटटाइम () तरीके प्रदान करती है।
इस पैकेज की मावेन फाइल निम्नलिखित है -
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
उदाहरण
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