Hi All:
I have an existing Java application which works fine. I want to add
some code to the application just to calculate the run time of the
program.
Its a Java Console program.
So, at the very start of the program, I want to get the current
date/time and store it in a variable.
Then, at the very last line of the program, I will again get the
current data/time and store it in a variable.
What's the best way to get the current date/time in Java and what's
the easiest way to find the difference in time in the two dates?
My existing application may sometimes take a few seconds and other
times runs in less than a second. So, my output probably needs to go
down to the millisecond level.
Any help is much appreciated!
Thanks!
Drew
Vova Reznik - 25 Jan 2006 15:36 GMT
> Hi All:
>
[quoted text clipped - 21 lines]
> Thanks!
> Drew
long start = System.currentTimeMillis();
...
long finish = System.currentTimeMillis();
System.out.println("It took " + (finish - start) + " millis to exec");
Drew - 25 Jan 2006 16:42 GMT
Thanks Vova! This works perfectly! Is milliseconds the smallest unit
of time supported in Java?
Drew
>> Hi All:
>>
[quoted text clipped - 26 lines]
>long finish = System.currentTimeMillis();
>System.out.println("It took " + (finish - start) + " millis to exec");
Vova Reznik - 25 Jan 2006 16:52 GMT
> Thanks Vova! This works perfectly! Is milliseconds the smallest unit
> of time supported in Java?
Google java nanoseconds
Babu Kalakrishnan - 25 Jan 2006 18:04 GMT
>> Thanks Vova! This works perfectly! Is milliseconds the smallest unit
>> of time supported in Java?
>
> Google java nanoseconds
This might point to the System.nanoTime() method - but you should also
read the fine print (the API documentation) which specifies that the
accuracy and granularity of this value is not guaranteed and is platform
specific. On my Centrino Laptop (with JDK 1.5.0_04) I haven't seen any
values returned by this method that dont end in a "000" (Which implies
at best a microsecond granularity whereas I would have expected better
if the method were using the TSC counter of the CPU - i.e. the RDTSC
instruction)
BK
Roedy Green - 25 Jan 2006 20:07 GMT
>Thanks Vova! This works perfectly! Is milliseconds the smallest unit
>of time supported in Java?
see http://mindprod.com/jgloss/time.html
for your options for finer resolution.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.