I've been looking at classes <Time>, <Date>, and <Calendar>, trying to
figure out how to time how long it takes my program to execute, but
without any luck. What I want to do is store a time value at the be-
ginning of my program, and then store a time value at the end of my
program, and then at the end of my program subtract the two values and
come up with how many milliseconds passed while the program was exe-
cuting.
For a bit I thought I could simply call the <getTime()> method of
class <Date> twice to accomplish this, since that method is supposed
to give the number of milliseconds transpired since 1 January 1970,
but no matter how long it takes for my program to run when I subtract
the two values I always get zero.
Does anyone know what I can do to time my Java program? Any informa-
tion anyone can give me would be greatly appreciated.
---Kevin Simonson
Stefan Ram - 18 Jul 2006 00:53 GMT
>Does anyone know what I can do to time my Java program?
I have written a method "timethese", that will print
the run time of all methods of a class to stdout.
A simple and a more elaborate, but still imperfect,
implementation can be found on page with the following URI.
http://www.purl.org/stefan_ram/pub/timethese-in-java
Lionel - 18 Jul 2006 01:09 GMT
> I've been looking at classes <Time>, <Date>, and <Calendar>, trying to
> figure out how to time how long it takes my program to execute, but
[quoted text clipped - 9 lines]
> but no matter how long it takes for my program to run when I subtract
> the two values I always get zero.
I bet that's because you are using int. You should use a long to store
the result of getTime().
> Does anyone know what I can do to time my Java program? Any informa-
> tion anyone can give me would be greatly appreciated.
If you use Netbeans IDE you can download a profiler that will do all the
hard work for you.
Lionel.
Mike Schilling - 18 Jul 2006 01:21 GMT
> I've been looking at classes <Time>, <Date>, and <Calendar>, trying to
> figure out how to time how long it takes my program to execute, but
[quoted text clipped - 3 lines]
> come up with how many milliseconds passed while the program was exe-
> cuting.
Sure; use System.currentTimeMillis(), e.g
public class Timeit
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++)
{
new Object();
}
long end = System.currentTimeMillis();
System.out.println(end-start + " milliseconds");
}
}
Thomas Fritsch - 18 Jul 2006 01:40 GMT
> I've been looking at classes <Time>, <Date>, and <Calendar>, trying to
> figure out how to time how long it takes my program to execute, but
[quoted text clipped - 3 lines]
> come up with how many milliseconds passed while the program was exe-
> cuting.
So far OK.
> For a bit I thought I could simply call the <getTime()> method of
> class <Date> twice to accomplish this, since that method is supposed
> to give the number of milliseconds transpired since 1 January 1970,
OK, but even simpler would be calling System.currentTimeMillis() twice and
subtracting the two values.
> but no matter how long it takes for my program to run when I subtract
> the two values I always get zero.
Where in your code did you get the end-time of your program?
You should do it just before you call System.exit(). This is *not*
necessarily equivalent to doing it at the end of your main method.
Consider a typical GUI-application. The following example would be *wrong*:
public static long startTime = System.currentTimeMillis();
public static void main(String args[]) {
showGUI();
System.out.println((System.currentTimeMillis() - startTime) + "
millisec");
}
It would print "0 millisec" or little more, because showGUI() immediately
returns after the GUI has popped up. But the GUI may run further for hours,
until the user exits the application.
> Does anyone know what I can do to time my Java program? Any informa-
> tion anyone can give me would be greatly appreciated.

Signature
Thomas
Thomas Fritsch - 18 Jul 2006 02:12 GMT
> I've been looking at classes <Time>, <Date>, and <Calendar>, trying to
> figure out how to time how long it takes my program to execute, but
[quoted text clipped - 9 lines]
> but no matter how long it takes for my program to run when I subtract
> the two values I always get zero.
Although you get a milliseconds value, that usually doesn't mean that this
value is accurate by 1 millisec. Depending on your OS you might always get a
multiple of say 50, because the system clock ticks once every 50 millisecs.
See also the API doc of System.currentTimeMillis()

Signature
Thomas
Patricia Shanahan - 18 Jul 2006 02:17 GMT
> I've been looking at classes <Time>, <Date>, and <Calendar>, trying to
> figure out how to time how long it takes my program to execute, but
[quoted text clipped - 14 lines]
>
> ---Kevin Simonson
Does your program do much? If not, the timer used for
System.currentTimeMillis() and equivalent, may not tick while it is
running. If that is your problem, you can fix it by either doing more
work in the program, so that it takes long enough to care about, or
using System.nanoTime().
This example program:
public class TestTime {
public static void main(String[] args) {
long n1;
long n2;
long m1;
long m2;
m1 = System.currentTimeMillis();
n1 = System.nanoTime();
n2 = System.nanoTime();
m2 = System.currentTimeMillis();
System.out.println("Nanoseconds " + (n2 - n1));
System.out.println("Milliseconds " + (m2 - m1));
}
}
Produces results like:
Nanoseconds 3882
Milliseconds 0
Patricia
Thomas Hawtin - 18 Jul 2006 13:40 GMT
> For a bit I thought I could simply call the <getTime()> method of
> class <Date> twice to accomplish this, since that method is supposed
> to give the number of milliseconds transpired since 1 January 1970,
> but no matter how long it takes for my program to run when I subtract
> the two values I always get zero.
Did you call getTime on the same Date instance? The no-arg Date
constructor takes a snapshot of the current time.
You could use new Date().getTime() twice. Better is System.nanoTime
which can measure shorter intervals. The millisecond timer (new Date()
or System.currentTimeMillis) can be as course as tens of milliseconds.
System.nanoTime should have a granularity of a few microseconds,
depending upon platform. It should also not be affected by changes to
the system clock (for instance, through ntp).
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
kvnsmnsn@hotmail.com - 18 Jul 2006 22:18 GMT
Thanks to everybody who replied! I ended up using
<System.currentTimeMillis()>, although
if that turns out to be too coarse a measure I'll use
<System.nanoTime()>.