What's the Best Way to Time a Program's Execution?

K

kvnsmnsn

I would like to tell how long it takes a piece of code I've written to
execute. I thought class <Date> might help me with that, so to test
that I wrote the following snippet of code:

import java.util.Date;

public class Loop
{
public static void main ( String[] arguments)
{
Date time;
long start, loops, lp;

if (arguments.length == 1)
{ loops = 1000000 * Long.parseLong( arguments[ 0]);
time = new Date();
start = time.getTime();
for (lp = 0; lp < loops; lp++);
System.out.println
( loops + " loops last " + (time.getTime() - start)
+ " milliseconds.");
}
else
{ System.out.println
( "Usage is\n java Loop <millions-of-loops>");
}
}
}

But apparently once I initialize <time> using the <Date()> construc-
tor, its <getTime()> method always returns the same value, so I always
get zero printed out.

Now I'm guessing that if I created two <Date> objects with that con-
structor, one before the loop and one after, then I'd get the number
of milliseconds I want. Is that the only way to time my loop's opera-
tion, or am I missing something?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
J

JeffLowery

Use Calendar:

Calendar cal =
Calendar.getInstance().getTime();

then
cal.clear();
cal.getTime();
 
O

opalpa

import java.util.*;
import java.text.*;
/**
timing tool
*/
public final class Stopwatch {
private String startedStr, stoppedStr;
private long startedAt, stoppedAt, elapsed;
final static private SimpleDateFormat format
= new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
public Stopwatch() {
stoppedStr = "not yet stopped";
stoppedAt = -1;
elapsed = -1;
startedAt = System.currentTimeMillis();
startedStr = format.format(startedAt);
}
public String toString() {
return "Stopwatch("+startedStr+", "+stoppedStr+",
"+elapsedSeconds()+" seconds)";
}
public double elapsedSeconds() {
if (stoppedAt == -1)
throw new IllegalStateException("not stopped");
double secs = ((double)elapsed) / 1000.0;
return secs;
}
synchronized public long stop() {
if (stoppedAt != -1)
throw new IllegalStateException("already stopped");
stoppedAt = System.currentTimeMillis();
stoppedStr = format.format(stoppedAt);
elapsed = stoppedAt - startedAt;
return elapsed;
}
}

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
R

Real Gagnon

I would like to tell how long it takes a piece of code I've written to
execute. I thought class <Date> might help me with that, so to test
that I wrote the following snippet of code:

long start = System.currentTimeMillis();
....
long end = System.currentTimeMillis();

long duration = end - start;
System.out.println(duration + " ms ");

On PC, the time resolution is not to high, used to be around 55ms.

Bye.
 
L

Lee Weiner

I would like to tell how long it takes a piece of code I've written to
execute. I thought class <Date> might help me with that, so to test
that I wrote the following snippet of code:

But apparently once I initialize <time> using the <Date()> construc-
tor, its <getTime()> method always returns the same value, so I always
get zero printed out.

Now I'm guessing that if I created two <Date> objects with that con-
structor, one before the loop and one after, then I'd get the number
of milliseconds I want. Is that the only way to time my loop's opera-
tion, or am I missing something?

There's an easier way. The System class has a static method
"currentTimeMillis()", that returns a long containing the number of
milliseconds that have elapsed since midnight. Call it once at the beginning,
call it again at the end, and subtract. As long as you're not performing the
test at exactly midnight, you shouldn't have a problem.

Lee Weiner
lee AT leeweiner DOT org
 
O

opalpa

The System class has a static method
"currentTimeMillis()", that returns a long containing the number of
milliseconds that have elapsed since midnight.

midnight, January 1, 1970 UTC.
As long as you're not performing the
test at exactly midnight, you shouldn't have a problem.

The method does not reset at midnight. Using at midnight is fine.

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
O

Oliver Wong

I would like to tell how long it takes a piece of code I've written to
execute.

You want the "Best Way"? Don't use java; use the facilities in your
operating system. Your OS can take into account factors such as your program
not getting 100% of the CPU, but rather sharing it with other processes, for
example. OSes have to time the execution of programs anyway to do
time-slicing so that every program gets a chance at the CPU. Check the
documentation for your OS to see what facilities are available to you.

- Oliver
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top