Re: How Do You Tell How Long it Takes for a Piece of Code to Execute?
- From: "Thomas Fritsch" <i.dont.like.spam@xxxxxxxxxxx>
- Date: Tue, 18 Jul 2006 02:41:57 +0200
<kvnsmnsn@xxxxxxxxxxx> wrote:
I've been looking at classes <Time>, <Date>, and <Calendar>, trying toSo far OK.
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.
OK, but even simpler would be calling System.currentTimeMillis() twice and
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,
subtracting the two values.
but no matter how long it takes for my program to run when I subtractWhere in your code did you get the end-time of your program?
the two values I always get zero.
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.
Thomas
.
- References:
- Prev by Date: Re: inner class and static
- Next by Date: Re: How Do You Tell How Long it Takes for a Piece of Code to Execute?
- Previous by thread: Re: How Do You Tell How Long it Takes for a Piece of Code to Execute?
- Next by thread: Re: How Do You Tell How Long it Takes for a Piece of Code to Execute?
- Index(es):
Relevant Pages
|