Validity period

I am parsing a file with several actions, and I want to measure the time it takes to complete these actions.

What is the best way to do this and print the time?

+3
source share
5 answers

Update: If you don't mind using external libraries and using JDK 5+, Google Guava has a Stopwatch that uses System.nanoTime(). It is not related to the absolute operating time of a system or wall, but instead is only useful in calculating the elapsed time, but this is exactly what you want.

Otherwise, you can use System.currentTimeMillis()that returns the current system time in milliseconds as a long integer.

long start = System.currentTimeMillis();
// ... perform operations ...
long time = System.currentTimeMillis() - start;
System.out.println("Operation took " + time + "ms");

, , . Java.

, , , . , .

+5

:

long start = System.currentTimeMillis();
//perform action
System.out.println("It took " + (System.currentTimeMillis() - start) + "ms");
+1
long start=System.currentTimeMillis();
//your action code
long end=System.currentTimeMillis();

int durationInMillis=end-start;
+1

, , , :

long start = System.nanoTime();
//Your code here
long timeElapsed = System.nanoTime() - start;
System.out.println(timeElapsed);

+1
+1

All Articles