How Do You Tell How Long it Takes for a Piece of Code to Execute?

K

kvnsmnsn

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
 
L

Lionel

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.

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.
 
M

Mike Schilling

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.

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");
}
}
 
T

Thomas Fritsch

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. 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.
 
T

Thomas Fritsch

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.
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()
 
P

Patricia Shanahan

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

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
 
T

Thomas Hawtin

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
 
K

kvnsmnsn

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()>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top