Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > currentTimeMillis

Reply
Thread Tools

currentTimeMillis

 
 
Aaron Fude
Guest
Posts: n/a
 
      05-28-2004
Hi,

Time to satisfy my curiosity. The output of the following program
public static void main(String[] inArgv) {
int N = 1000000;
long[] times = new long[N];

for (int i = 0; i < N; i++)
times[i] = System.currentTimeMillis();

for (int i = 0; i < 10; i++)
System.out.println(times[i*(N/10)]);
}

....is

1085719861549
1085719861559
1085719861569
1085719861580
1085719861590
1085719861600
1085719861610
1085719861620
1085719861620
1085719861630

It seems that the java clock ticks in 10's of milliseconds. Is there
anything interesting anyone can say about this?

Aaron Fude



 
Reply With Quote
 
 
 
 
Elie De Brauwer
Guest
Posts: n/a
 
      05-28-2004
On Fri, 28 May 2004 01:00:04 -0400, Aaron Fude wrote:

> Hi,
>
> Time to satisfy my curiosity. The output of the following program
> public static void main(String[] inArgv) {
> int N = 1000000;
> long[] times = new long[N];
>
> for (int i = 0; i < N; i++)
> times[i] = System.currentTimeMillis();
>
> for (int i = 0; i < 10; i++)
> System.out.println(times[i*(N/10)]);
> }
>
> ...is
>
> 1085719861549
> 1085719861559
> 1085719861569
> 1085719861580
> 1085719861590
> 1085719861600
> 1085719861610
> 1085719861620
> 1085719861620
> 1085719861630
>
> It seems that the java clock ticks in 10's of milliseconds. Is there
> anything interesting anyone can say about this?
>
> Aaron Fude


Well, I can't but the api can , see
http://java.sun.com/j2se/1.5.0/docs/...entTimeMillis()

Where there is stated:
"Returns the current time in milliseconds. Note that while the unit of
time of the return value is a millisecond, the granularity of the value
depends on the underlying operating system and may be larger. For example,
many operating systems measure time in units of tens of milliseconds."

On my GNU/Linux box I get the following output:
1085720831892
1085720831968
1085720832038
1085720832108
1085720832177
1085720832247
1085720832317
1085720832387
1085720832457
1085720832527

And if I alter the for loop I can output any individual tick.

hth

--
Elie De Brauwer
http://www.de-brauwer.be

No animals were hurt and no microsoft products were used during the
creation of this e-mail

 
Reply With Quote
 
 
 
 
Frank
Guest
Posts: n/a
 
      05-28-2004
This is an OS issue... AFAIKT, most OSs will cache the currentTimeMillis()
so that repeated calls won't slow down while the system checks with the
RTC. On windows, the timing resolution is around 10ms... I think I've
heard Linux folks claim they can adjust the default to be as sensative as
1 ms.

Hope this helps!

Frank

On Fri, 28 May 2004 01:00:04 -0400, Aaron Fude <(E-Mail Removed)> wrote:

> Hi,
>
> Time to satisfy my curiosity. The output of the following program
> public static void main(String[] inArgv) {
> int N = 1000000;
> long[] times = new long[N];
>
> for (int i = 0; i < N; i++)
> times[i] = System.currentTimeMillis();
>
> for (int i = 0; i < 10; i++)
> System.out.println(times[i*(N/10)]);
> }
>
> ...is
>
> 1085719861549
> 1085719861559
> 1085719861569
> 1085719861580
> 1085719861590
> 1085719861600
> 1085719861610
> 1085719861620
> 1085719861620
> 1085719861630
>
> It seems that the java clock ticks in 10's of milliseconds. Is there
> anything interesting anyone can say about this?
>
> Aaron Fude
>
>
>


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-28-2004
On Fri, 28 May 2004 01:00:04 -0400, "Aaron Fude" <(E-Mail Removed)>
wrote or quoted :

>
>It seems that the java clock ticks in 10's of milliseconds. Is there
>anything interesting anyone can say about this?



Java can't do anything about the granularity of the underlying system
clock.

The original PC's used a very crude tick timer where a tick is
65536/1193180 sec : 18.2065 per second.

I don't know if newer models are now using a more accurate clock.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      05-28-2004
Aaron Fude wrote:

> 1085719861549
> 1085719861559
> 1085719861569
> 1085719861580
> 1085719861590
> 1085719861600
> 1085719861610
> 1085719861620
> 1085719861620
> 1085719861630
>
> It seems that the java clock ticks in 10's of milliseconds. Is there
> anything interesting anyone can say about this?


You must have a very slow machine for it to take 10 milliseconds for each
iteration of such a simple loop.



-- chris


 
Reply With Quote
 
Lee Fesperman
Guest
Posts: n/a
 
      05-28-2004
Roedy Green wrote:
>
> On Fri, 28 May 2004 01:00:04 -0400, "Aaron Fude" <(E-Mail Removed)>
> wrote or quoted :
>
> >
> >It seems that the java clock ticks in 10's of milliseconds. Is there
> >anything interesting anyone can say about this?

>
> Java can't do anything about the granularity of the underlying system
> clock.
>
> The original PC's used a very crude tick timer where a tick is
> 65536/1193180 sec : 18.2065 per second.
>
> I don't know if newer models are now using a more accurate clock.


Actually, it's the way Microsoft programs the clock. The clocks on PC's provide sub-ms
granularity. I know, I wrote a real-time operating system that ran with DOS and
reprogrammed the clock to under a ms. It then interrupted DOS at the interval it
expected.

--
Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
 
Reply With Quote
 
Aaron Fude
Guest
Posts: n/a
 
      05-28-2004

"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:(E-Mail Removed)...
> Aaron Fude wrote:
>
> > 1085719861549
> > 1085719861559
> > 1085719861569
> > 1085719861580
> > 1085719861590
> > 1085719861600
> > 1085719861610
> > 1085719861620
> > 1085719861620
> > 1085719861630
> >
> > It seems that the java clock ticks in 10's of milliseconds. Is there
> > anything interesting anyone can say about this?

>
> You must have a very slow machine for it to take 10 milliseconds for each
> iteration of such a simple loop.
>
>


Oh yeah? Aren't I printing out every 100000th iteration?

>
> -- chris
>
>



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-29-2004
> It seems that the java clock ticks in 10's of milliseconds. Is there
> anything interesting anyone can say about this?


Hint: Microsoft Windows
Bigger Hint: API Specification
Further Reference: http://www.google.com

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-29-2004
On Sat, 29 May 2004 13:36:38 +1000, "Tony Morris"
<(E-Mail Removed)> wrote or quoted :

>> It seems that the java clock ticks in 10's of milliseconds. Is there
>> anything interesting anyone can say about this?


If you want something with very fine resolution consider the RDTSC
instruction and some JNI.

Somebody surely has cooked up the trivial JNI to access it. If not,
I'll do it for $25 US.

The problem is there is high overhead to the JNI call.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      05-29-2004


"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 29 May 2004 13:36:38 +1000, "Tony Morris"
> <(E-Mail Removed)> wrote or quoted :
>
> >> It seems that the java clock ticks in 10's of milliseconds. Is there
> >> anything interesting anyone can say about this?


I believe the resolution of time used in NTP is
something like 128 picoseconds. Maybe we
should be using that. Actually, I don't know what
the meaning of 'java click ticks' is, normally I
think about hardware and ticks, not software and ticks.

>
> If you want something with very fine resolution consider the RDTSC
> instruction and some JNI.
>
> Somebody surely has cooked up the trivial JNI to access it. If not,
> I'll do it for $25 US.
>
> The problem is there is high overhead to the JNI call.
>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is java's System.currentTimeMillis() in python neoedmund Python 1 09-08-2006 03:08 AM
Random numbers seeded with System.currentTimeMillis Roedy Green Java 8 03-09-2006 10:59 PM
System.currentTimeMillis gives wrong time Alex Java 15 09-08-2005 08:25 PM
System.currentTimeMillis() Jerry Java 18 08-06-2005 02:52 PM
System.currentTimeMillis not returning GMT/UTC (J2me) Nelson Java 17 05-19-2004 11:56 PM



Advertisments