Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the running time of my program? [duplicate]

I wrote a program and now I want to calculate the total running time of my program from start to end.

How can I do this?

like image 892
Mahdi_Nine Avatar asked Mar 05 '11 13:03

Mahdi_Nine


People also ask

How do you calculate run time of a program?

To calculate the running time, find the maximum number of nested loops that go through a significant portion of the input. Some algorithms use nested loops where the outer loop goes through an input n while the inner loop goes through a different input m. The time complexity in such cases is O(nm).

What is running time of a program?

Run time is a phase of a computer program in which the program is run or executed on a computer system. Run time is part of the program life cycle, and it describes the time between when the program begins running within the memory until it is terminated or closed by the user or the operating system.

How do you record time in Java?

Using Instant. Instant class can be used to record event time-stamps in the application. It has now() method that obtains the current instant from the system clock. We can convert this instant to the total number of milliseconds using toEpochMilli() method. Please note that Instant.


1 Answers

Use System.nanoTime to get the current time.

long startTime = System.nanoTime(); .....your program.... long endTime   = System.nanoTime(); long totalTime = endTime - startTime; System.out.println(totalTime); 

The above code prints the running time of the program in nanoseconds.

like image 91
snakile Avatar answered Oct 12 '22 15:10

snakile