trs
trs

Reputation: 2454

Subtraction of System.currentTimeMillis() yielding zero

I am trying to keep track of time in my program using System.currentTimeMillis() However, when I try to subtract the start time from the end time the result is zero when I know it should not be. I think it may have to do with the declared datatype or type-casting that may be necessary. How can I fix it to subtract properly and convert the result to seconds?

See code:

long start=System.currentTimeMillis();
//some code executes....
    long end=System.currentTimeMillis();
    long result=end-start;
double seconds= TimeUnit.MILLISECONDS.toSeconds(result);

Upvotes: 0

Views: 3002

Answers (5)

Nigel Sheridan-Smith
Nigel Sheridan-Smith

Reputation: 785

System.currentTimeMillis() depends on your OS platform and may not be granular enough between method calls to capture the time period you want. Calling it twice, one immediately after-the-other, is unlikely to give you sufficient enough time difference.

You can execute your code one thousand times and measure this time difference, or use a profiling tool such as JProfiler, although these typically only measure the time required across a particular method call.

There are some native JNI libraries for Java 1.4 and Java 5 additional methods (getProcessCPUTime) for capturing more granular timestamps but I cannot find the specific details at the moment.

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

No, your code is fine. Most likely a millisecond just isn't elapsing. Try using System.nanoTime() instead.

EDIT: Actually, there is an issue with the code. Per the TimeUnit.convert docs, "Conversions from finer to coarser granularities truncate, so lose precision", so if result is less than 1000, it will be 0.

If you want fractional seconds, just do:

double seconds = result / 1000.0;

nanoTime could still be useful, but it might not be an issue if the code takes long enough.

Upvotes: 3

Edwin Buck
Edwin Buck

Reputation: 70999

Most systems can't resolve a single millisecond. It is common for each clock tick to be in the 100 to 200 millisecond range. This means you get "chunks" of milliseconds passing. That makes it nearly impossible to time something as quick as your program. Odds are it is starting and ending before another clock tick is registered.

You need to use System.nanotime() as suggested elsewhere. Also don't convert a fraction of a second into a second expecting a full second as a result. Well, that is unless you round UP.

Upvotes: 2

ObscureRobot
ObscureRobot

Reputation: 7336

That is expected - it will take far less than a second to execute your first two lines that set start and end. So when you truncate the difference down to a second, you get zero.

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160321

You're not really doing much between the start and end, and it's not a terribly accurate timer when dealing with really tiny amounts of time. Try actually doing something in-between.

Upvotes: 0

Related Questions