Re: How Do You Tell How Long it Takes for a Piece of Code to Execute?
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Tue, 18 Jul 2006 01:17:54 GMT
kvnsmnsn@xxxxxxxxxxx wrote:
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
.
- References:
- Prev by Date: Re: java.lang.SecurityException: Prohibited package name: java
- Next by Date: Re: JNI problems, dll generation with Visual studio 8, repost
- Previous by thread: Re: How Do You Tell How Long it Takes for a Piece of Code to Execute?
- Next by thread: Class dependency problem
- Index(es):
Relevant Pages
|