Measuring a short interval in time

W

Wes Harrison

I would like to take two points in my program and measure the elapsed time
between them. Currently I am doing:

Calendar cal1 = Calendar.getInstance();
long time1 = cal1.getTimeInMillis());
..
..
Calendar cal2 = Calendar.getInstance();
long time2 = cal2.getTimeInMillis());

long diff = time2 - time1;

Is there a better, simpler or faster way of doing this? I want the elapsed
time in milliseconds.

Thanks,

Wes
 
M

Michael Borgwardt

Wes said:
I would like to take two points in my program and measure the elapsed time
between them. Currently I am doing:

Calendar cal1 = Calendar.getInstance();
long time1 = cal1.getTimeInMillis());
.
.
Calendar cal2 = Calendar.getInstance();
long time2 = cal2.getTimeInMillis());

long diff = time2 - time1;

Is there a better, simpler or faster way of doing this? I want the elapsed
time in milliseconds.

No need for the weighty Calendar objects, just use System.currentTimeMillis() directly
 
P

Paul Lutus

Wes said:
I would like to take two points in my program and measure the elapsed time
between them. Currently I am doing:

Calendar cal1 = Calendar.getInstance();
long time1 = cal1.getTimeInMillis());
.
.
Calendar cal2 = Calendar.getInstance();
long time2 = cal2.getTimeInMillis());

long diff = time2 - time1;

Is there a better, simpler or faster way of doing this?

Not really, not in a functional sense. Another poster has pointed out the
use of System.currentTimeMillis(), but the result is the same.
I want the
elapsed time in milliseconds.

You do realize, don't you, that in a multitasking operating system, you
won't get what you may want, e.g. the actual processor time to accomplish a
task? Also, the millisecond times provided by typical PCs is granular,
sometimes to a degree that your results may not be accurate enough.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top