CPU-Zeit analysieren
Du optimierst eine Anwendung zur Finanzanalyse, die Berechnungen durchführt. Um die Prozessorlast genau zu verstehen, sollst du ein Tool implementieren, das die exakte CPU-Zeit misst, die von verschiedenen Operationen verbraucht wird. So findest du heraus, welche Berechnungen wirklich CPU-intensiv sind.
Die benötigten lang.management-Pakete wurden bereits für dich importiert.
Diese Übung ist Teil des Kurses
Codeoptimierung in Java
Anleitung zur Übung
- Hole die
ThreadMXBean-Instanz aus der KlasseManagementFactory. - Ermittle die CPU-Zeit für den aktuellen Thread.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
public class Main {
public static long getCpuTimeNano() {
// Retrieve the ThreadMXBean instance
ThreadMXBean threadMXBean = ____.____()
if (threadMXBean.isThreadCpuTimeSupported()) {
threadMXBean.setThreadCpuTimeEnabled(true);
// Return the current thread CPU time
return ____(Thread.currentThread().threadId());
} else {
System.out.println("CPU time measurement not supported");
return 0;
}
}
public static long calculateSum(long n) {
long sum = 0;
for (long i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
public static long calculateSquareSum(long n) {
long sum = 0;
for (long i = 1; i <= n; i++) {
sum += i * i;
}
return sum;
}
public static void main(String[] args) {
long n = 100_000_000; // 100 million
long startSum = getCpuTimeNano();
long sum = calculateSum(n);
long endSum = getCpuTimeNano();
System.out.println("CPU time for sum: " +
((endSum - startSum) / 1_000_000.0) + " ms");
long startSquareSum = getCpuTimeNano();
long squareSum = calculateSquareSum(n);
long endSquareSum = getCpuTimeNano();
System.out.println("CPU time for square sum: " +
((endSquareSum - startSquareSum) / 1_000_000.0) + " ms");
double ratio = (endSquareSum - startSquareSum) / (double)(endSum - startSum);
System.out.println("Square sum takes " + ratio +
" times more CPU time than regular sum");
}
}