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_
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_