smatter
smatter

Reputation: 29178

Java: What is the unit of the difference of two System.currentTimeMillis?

What is the unit of the difference of two System.currentTimeMillis ?

start = System.currentTimeMillis();
longoperation();
elapsedTime = System.currentTimeMillis() - start;

What is the unit of elapsed time here. It doesn't look like milliseconds. Is the above code segment the right way to find the time taken to execute longoperation()?

Upvotes: 2

Views: 2809

Answers (5)

Ian Parkinson
Ian Parkinson

Reputation: 131

Yes, currentTimeMillis() returns you a milliseconds value.

On Windows, it used to be the case that the returned value had quite low resolution, and so was only accurate to something like 10ms. I'm not certain whether this is still the case as I haven't used Windows for a few years - but if your longoperation() actually takes just a few millis, and you're running on Windows, then you may see elapsedTime variously being 10ms or 0ms.

Upvotes: 2

bragboy
bragboy

Reputation: 35542

Whatever you are doing is correct. If you want the time in seconds, simply divide it by 1000.

long start = System.currentTimeMillis();
longoperation();
long elapsedTime = (System.currentTimeMillis() - start)/1000;

Upvotes: 3

Boris Pavlović
Boris Pavlović

Reputation: 64632

Yes, it is in milliseconds. Bear in mind that the difference is not absolutely correct and may vary.

Upvotes: 5

Jigar Joshi
Jigar Joshi

Reputation: 240908

It is ms (MiliSecond) only, you are doing right.
You can ignore time taken while calculating millis

Upvotes: 4

Cameron Skinner
Cameron Skinner

Reputation: 54326

It is milliseconds and your code looks correct.

Upvotes: 3

Related Questions