How to check the total time & total space usage in Java
To check the total time it takes to execute a piece of code in Java, you can use the System.currentTimeMillis() method. Here's an example:
long startTime = System.currentTimeMillis(); // Code to be measured goes here long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; System.out.println("Total time to execute: " + totalTime + " milliseconds");
startTime and endTime is calculated and printed in milliseconds. You can adjust the units of time as needed.Runtime class, which provides methods to interact with the JVM's runtime environment. Here is an example of how to get the total amount of memory that is currently in use:totalMemory gives the size of the current heap, while usedMemory is the actual amount of memory used by the application.
No comments:
Post a Comment