I suddenly wanted to accurately test the Java code execution time and found it online for a while. There are two methods to discover:
The first type is measured in milliseconds.
Java code
- // Pseudocode
- Long starttime = system. currenttimemillis (); // get the start time
- Dosomething (); // code snippet for testing
- Long endtime = system. currenttimemillis (); // get the end time
- System. Out. println ("program running time:" + (end-Start) + "Ms ");
// Pseudocode long starttime = system. currenttimemillis (); // get the start time dosomething (); // test the code segment long endtime = system. currenttimemillis (); // obtain the end time system. out. println ("program running time:" + (end-Start) + "Ms ");
The second type is calculated in nanoseconds.
Java code
- // Pseudocode
- Long starttime = system. nanotime (); // get the start time
- Dosomething (); // code snippet for testing
- Long endtime = system. nanotime (); // get the end time
- System. Out. println ("program running time:" + (end-Start) + "ns ");