measuring time intervals precisely

measuring time intervals precisely

Post by Luc » Sat, 06 Apr 2002 17:36:07



If I write a piece of java code like this:
.
.
.
long start = System.currentTimeMillis();
Thread.sleep(2);
long end = System.currentTimeMillis();
System.out.println((end  - start));
.
.
.

on Windows 98 I always get values greater than 10 ms
on Windows NT I usually get a value of 10 ms
on Linux RedHat7.2 I always get vaules close to 20 ms

I run this test with the JVM running simply the current thread

The questions are:

is System.currentTimeMillis() accurate in measuring time intervals?

if not, is there a way to measure precisely time intervals?

 
 
 

measuring time intervals precisely

Post by Nico Kadel-Garci » Sat, 06 Apr 2002 22:59:57



Quote:> If I write a piece of java code like this:
> .
> .
> .
> long start = System.currentTimeMillis();
> Thread.sleep(2);
> long end = System.currentTimeMillis();
> System.out.println((end  - start));
> .
> .
> .

> on Windows 98 I always get values greater than 10 ms
> on Windows NT I usually get a value of 10 ms
> on Linux RedHat7.2 I always get vaules close to 20 ms

> I run this test with the JVM running simply the current thread

> The questions are:

> is System.currentTimeMillis() accurate in measuring time intervals?

> if not, is there a way to measure precisely time intervals?

Not with that kind of resolution: this is one of those cases where the
underlying OS really matters, since they have different available time
measurement frequencies and perhaps even ways of rounding off time
measurements. As cool as Java's virtual machine is, it can't get completely
away from the underlying structure.

 
 
 

measuring time intervals precisely

Post by George Hube » Mon, 08 Apr 2002 05:56:10


Hi,

Assuming you are running with an Intel chip, there is an instruction:
RDTSC (read time stamp) which returns the number of CPU
cycles since boot.  Using this you can get time intervals
down to nanosecond precision.

However, the down side to this is that you will need to use
assembly to access this particular command.

George


Quote:> If I write a piece of java code like this:
> .
> .
> .
> long start = System.currentTimeMillis();
> Thread.sleep(2);
> long end = System.currentTimeMillis();
> System.out.println((end  - start));
> .
> .
> .

> on Windows 98 I always get values greater than 10 ms
> on Windows NT I usually get a value of 10 ms
> on Linux RedHat7.2 I always get vaules close to 20 ms

> I run this test with the JVM running simply the current thread

> The questions are:

> is System.currentTimeMillis() accurate in measuring time intervals?

> if not, is there a way to measure precisely time intervals?