• Home
  • RSS Feed
  • Log in

Best of Xebia tech list July 2007: Accuracy of System.getCurrentTimeMillis()
Posted by Erwin van der Koogh in the early afternoon: August 3rd, 2007

At Xebia we have an internal mailing list where a lot of interesting technical discussions take place. Because we have a passion for knowledge sharing, we thought we'd give you a glimpse of some recent topics. This is the first of a series we plan to do.

We choose this particular mail thread because it answers a technical question a lot of people might have. And it also introduces a little known feature of Java 5.

From Jan Vermeir
Subject Accuracy of System.getCurrentTimeMillis()
Date Mon 23/07/2007 15:51

Hi, we're measuring elapsed time for processing a message. each measured operation takes a couple of milliseconds, so I was wondering what the accuracy of System.getCurrentTimeMillis() is. I have some vague recollection about accuracy not actually being milliseconds, as the name would suggest, and that it may vary with platform and JVM vendor.

Does anyone know for sure?

Jan.

From Jeroen Borgers
Subject Re: Accuracy of System.getCurrentTimeMillis()
Date Mon 23/07/2007 15:54

The timing granularity is dependent on the operating system, many operating systems measure time in units of tens of milliseconds. I am not aware of different VM's acting differently here.

Since Java 5 there is the API call System.nanoTime() which can give you more accuracy, it returns the current value of the most precise available system timer, in nanoseconds. It provides nanosecond precision, but not necessarily nanosecond accuracy, again dependent on the OS. This one is only for relative comparisons, within the same VM.

What for do you use this? With benchmarking, you usually want to time long enough periods only, like seconds, so you don't have to bother about this accuracy.

Regards,
Jeroen.

From Machiel Groeneveld
Subject Re: Accuracy of System.getCurrentTimeMillis()
Date Tue 24/07/2007 10:36

Might be of interest: http://mindprod.com/jgloss/time.html

From Jeroen Borgers
Subject Re: Accuracy of System.getCurrentTimeMillis()
Date Tue 24/07/2007 11:06

Some additional info:

Here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6440250 it states that on Windows XP, getNanoTime is more accurate than currentTimeMillis, but it is more costly to execute. So, don't use it too much.

And on
http://forum.java.sun.com/thread.jspa?threadID=5119176&messageID=9413087
some more info on Linux:

On Linux things seem to be more distinct. In order to perform
System.nanoTime() as specified, the VM needs a monotonic clock and processes clock_gettime. If CLOCK_MONOTONIC is not supported then there is no monotonic clock available for the VM and System.nanoTime() behaves as gettimeofday. Gettimeofday measures with microseconds accuracy.

Regards,
Jeroen.

  • Share/Bookmark

Filed under Java, Techlist | No Comments »



No Responses to “Best of Xebia tech list July 2007: Accuracy of System.getCurrentTimeMillis()”



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    Xebia Java Architecture Ajax fitnesse Agile Awareness Workshop Seam Introduction to Agile Lean Hibernate XML Functional Programming Maven Poppendieck Spring JavaOne Grails Closures esb Performance Scala IntelliJ Scrum product owner Groovy qcon SOA Semantic Web Agile Testing