ऑपरेशन का समय मापना
String concatenation एक सामान्य ऑपरेशन है जो कुशलता से न किया जाए तो परफॉर्मेंस bottleneck बन सकता है. इस अभ्यास में, आप + ऑपरेटर से strings जोड़ने में लगने वाला समय मापेंगे.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में कोड ऑप्टिमाइज़ करना
अभ्यास निर्देश
- हमारे ऑपरेशन से ठीक पहले और बाद में शुरुआत और अंत का समय रिकॉर्ड करें.
startTimeऔरendTimeका उपयोग करके हमारे ऑपरेशन की कुल अवधि निकालें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
public class Main {
public static void main(String[] args) {
// Get the start time
long startTime = System.____();
String result = "";
for (int i = 0; i < 10000; i++) {
// Add the current number to the result string
result += i;
}
// Get the end time
long endTime = ____.nanoTime();
// Calculate the duration
long durationInNanos = ____;
double durationInMillis = durationInNanos / 1_000_000.0;
System.out.println("String concatenation took: " + durationInMillis + " ms");
System.out.println("Final string length: " + result.length());
}
}