-1

I'm trying to get the execution time for an algorithm of a java program. I've looked around at similar questions and tried a few ways to do that, but when I print the time lapse I always get 0.

Instant start = Instant.now();
//Method I want to get the exec time
Instant end = Instant.now();

System.out.println("\nTime lapse: "+Duration.between(start, end));

Prints:

Time lapse: PT0S

As well as:

 Date d = new Date();
 long t1, t2;

 t1 = d.getTime();
 //Method I want to get the exec time
 t2 = d.getTime(); 

 System.out.println("\nTime lapse: "+(t2-t1));

Outputs:

 Time lapse: 0

Meanwhile, getting the exec time in C of the exact same method, gives a fair result:

#include <time.h>

clock_t start, end;
double time;

start = clock(); 
//Method I want to get the exec time
end = clock();

time = (double) (end-start) / CLOCKS_PER_SEC; 

printf("\nTime lapse: %f\n", time);

Outputs:

Time lapse: 0.000004

Why can't I do that in Java?

EDIT

I've tried to use getTimeInMillis() as suggested:

Calendar cal = Calendar.getInstance();
double start = cal.getTimeInMillis();

//Method I want to get the exec time

double end = cal.getTimeInMillis();

System.out.println("\nTime lapse: "+(end - start));

Outputs:

Time lapse: 0.0

FINAL EDIT: Ok so, the program did run in less than a millisecond, that's why I kept getting 0 as a result, and I solved thanks to @Ayo K advice, I used System.nanoTime() to finally get a result.

IDK
  • 359
  • 1
  • 16
  • 3
    for the second example its obvious. you create 1 `Date` object at the start and then you tell start and end to use the same value – XtremeBaumer Jan 18 '18 at 10:34
  • Why do you use `double` with `getTimeInMillis()`? Don't you pay any attention to the return types or methods you're using? – Kayaman Jan 18 '18 at 10:56
  • @IDK It seems like you just tested `Instant start = Instant.now();Instant end = Instant.now();System.out.println("\nTime lapse: "+Duration.between(start, end));` without anything to measure, in this case it's possible that the code runs in less than a millisecond. –  Jan 18 '18 at 11:01
  • @Kayaman the first try I did was with long type variables and it printed 0. Then I tried double and it outputs 0.0. – IDK Jan 18 '18 at 11:07
  • @devpuh the code is there, of course, but yeah probably takes less than a millisecond. – IDK Jan 18 '18 at 11:08

3 Answers3

1

For this to work, you have to get the time in milliseconds then get the difference.

You can use System.currentTimeMillis() for this. Then get the difference at the end.

EXAMPLE

long start = System.currentTimeMillis();
System.out.println("start: "+start);
long stop = System.currentTimeMillis();
System.out.println("stop: "+stop);
///convert to seconds
System.out.println((stop-start)/1000f);

RESULT:

0.057

EDIT IF the code runs in less than a Milli second you can use Nano seconds using System.nanoTime(). For simulation purposes you can call Thread.sleep(n) to create a delay where n is how long you intend to delay in milliseconds.

Ayo K
  • 1,719
  • 2
  • 22
  • 34
  • Please give an example of your solution – ItamarG3 Jan 18 '18 at 10:36
  • Actually he already did this in the first example. –  Jan 18 '18 at 10:38
  • @Ayo The reason he gets zero, is not because he uses the wrong method, but because he calls getTime() on the exact same instance. he should have called it on the original instance, and on a new instance created on termination of the method – Stultuske Jan 18 '18 at 10:40
  • @AyoK also read the information you put before the example: " For this to work, you have to get the time in milliseconds then get the difference. The application runs in less than a second that's why you get zero." > this is wrong. – Stultuske Jan 18 '18 at 10:46
  • @AyoK This code can still return "0.000" when it runs in less than a millisecond. So please add some code which needs some time to execute. e.g. `Thread.sleep(1);` –  Jan 18 '18 at 10:52
1

Example with Instant:

Instant s = Instant.now();
for (int i = 0; i < 100000; i++) {
    System.out.println(i);
}
Instant s1 = Instant.now();

System.out.println(s1.getNano() - s.getNano());
System.out.println(Duration.between(s, s1));

prints the times:

347000000
PT0.347S

and with Date:

Date d = new Date();
for (int i = 0; i < 100000; i++) {
    System.out.println(i);
}
Date d1 = new Date();

System.out.println(d1.getTime() - d.getTime());

prints:

389

now you only have to calculate the type you want (seconds, milliseconds or whatever). The docs will tell you what you get from the methods if the names a not self-explanatory

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

In the example using Date, you need to recreate the Date object (the constructor set's the time to be the current time):

int t1 = (new Date()).getTime();
//method you want to check
int t2 = (new Date()).getTime();

Or just use System.getCurrentTimeMillis() once before, then once after

ItamarG3
  • 4,092
  • 6
  • 31
  • 44