135

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?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117

6 Answers6

269

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.

snakile
  • 52,936
  • 62
  • 169
  • 241
52

At the beginning of your main method, add this line of code :

final long startTime = System.nanoTime();

And then, at the last line of your main method, you can add :

final long duration = System.nanoTime() - startTime;

duration now contains the time in nanoseconds that your program ran. You can for example print this value like this:

System.out.println(duration);

If you want to show duration time in seconds, you must divide the value by 1'000'000'000. Or if you want a Date object: Date myTime = new Date(duration / 1000); You can then access the various methods of Date to print number of minutes, hours, etc.

Rakib Ansary
  • 4,078
  • 2
  • 18
  • 29
krtek
  • 26,334
  • 5
  • 56
  • 84
27

Use System.currentTimeMillis() or System.nanoTime() if you want even more precise reading. Usually, milliseconds is precise enough if you need to output the value to the user. Moreover, System.nanoTime() may return negative values, thus it may be possible that, if you're using that method, the return value is not correct.

A general and wide use would be to use milliseconds :

long start = System.currentTimeMillis();

... 


long end = System.currentTimeMillis();

NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");

Note that nanoseconds are usually used to calculate very short and precise program executions, such as unit testing and benchmarking. Thus, for overall program execution, milliseconds are preferable.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • 1
    Beware that System.currentTimeMillis() is affected by changes of the system clock whereas System.nanoTime() isn't. So in case the system clock is set back 5 minutes by NTP the result with System.currentTimeMills() mabye 55 minutes whereas the result with System.nanoTime() may give 60 minutes. – Uli Feb 14 '19 at 08:11
4

The general approach to this is to:

  1. Get the time at the start of your benchmark, say at the start of main().
  2. Run your code.
  3. Get the time at the end of your benchmark, say at the end of main().
  4. Subtract the start time from the end time and convert into appropriate units.

A hint: look at System.nanoTime() or System.currentTimeMillis().

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • 1
    As an additional note: Since 1.5 you can use java.util.concurrent.TimeUnit for easy conversions from milliseconds and especially nano-seconds to other units. – Boris Mar 05 '11 at 13:43
2

You need to get the time when the application starts, and compare that to the time when the application ends.

Wen the app starts:

Calendar calendar = Calendar.getInstance();

// Get start time (this needs to be a global variable).
Date startDate = calendar.getTime();

When the application ends

Calendar calendar = Calendar.getInstance();

// Get start time (this needs to be a global variable).
Date endDate = calendar.getTime();

To get the difference (in millseconds), do this:

long sumDate = endDate.getTime() - startDate.getTime();
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
  • 6
    using Date and Calendar to obtain a duration in milliseconds seems a little bit "overkill" to me, System.currentTimeMillis() would be a better pick. And if you copy past all your lines in the main method, calendar will be defined two times -> compilation error – krtek Mar 05 '11 at 13:35
2

Beside the well-known (and already mentioned) System.currentTimeMillis() and System.nanoTime() there is also a neat library called perf4j which might be useful too, depending on your purpose of course.

drac_o
  • 427
  • 5
  • 11
Boris
  • 4,327
  • 2
  • 19
  • 27