JavaAntiPatterns

Collection of bad coding practices

Measure time intervals using System.currentTimeMillis()

with 6 comments

Well, what’s wrong with a common practice of measuring elapsed time in the code like this:

(bad)

long startTime = System.currentTimeMillis();
// ...
long elapsedTime = System.currentTimeMillis() - startTime;

The approach is based on an intuitive assumption that the values returned by currentTimeMillis() (in general, the current system time expressed as a number of milliseconds since midnight, January 1, 1970 UTC) grow smoothly and always are greater in the future than in the past. This is not always the case.

A simple example when this assumption fails is adjusting the system clock. On the most of the modern systems, correction of the clock is performed automatically by polling a value from the NTP server in a specific period of time. If this correction happens in the middle of your operation, the measured time interval will be wrong or even negative. Note that the typical computer clocks have an error in tens or even hundreds of milliseconds per hour that means that the correction offset will be up to few seconds if it is performed once a day.

It is safer to use the System.nanoTime() method which does not depend on a system clock, so it’s guaranteed that its value grows smoothly from the past to the future:

(good)

long startTime = System.nanoTime();
// ...
long elapsedTime = (System.nanoTime() - startTime) / 1000000; // ms

And remember that the nanoTime values has nothing to do with any real absolute time, so don’t try to use it for the Date objects, for instance.

See also: Inside the Hotspot VM: Clocks, Timers and Scheduling Events

Advertisements

Written by Alex

March 15, 2012 at 4:13 am

Posted in Threads

Tagged with

6 Responses

Subscribe to comments with RSS.

  1. One school free advice on how to get him back has dealt with immediately.

    Some just depend on having a normal but private activity for the unintentional mistake and
    the marriage and divorce or abreakup are certainly available.
    When working with her public yet fearing the prospect of romantic
    involvement with them powerfully influence his sense of neediness and inadequacy.
    That’s bewildering and we struck up some considerable time and see two people.

    Jeanne

    May 12, 2013 at 8:21 pm

  2. Nice article.

    I would recommend some improvements to following line:

    long elapsedTime = (System.nanoTime() – startTime) / 1000000; // ms

    better would be:

    long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() – startTime);

    Following problems:
    – Trailing comments are bad coding style
    – variable name could hold the unit
    – it is to easy to write the false number of zeros to ‘1000000’

    André

    January 16, 2014 at 2:09 pm

  3. Thank you very much, helps me a lot!

    Nilton

    May 17, 2014 at 10:00 pm

  4. Reblogged this on javabynataraj and commented:
    Good code snippet from Alex. He has given how to measure time intervals using the System.nanoTime method. Very good to practice.

    javabynataraj

    July 8, 2014 at 1:56 pm

  5. Thanks.
    After reading the java api again,
    http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()
    It seems nanoTime() is made specifically for measuring time intervals.

    mockthetest

    July 13, 2014 at 7:00 pm

  6. One HUGE drawback however is that nanoTime is a 100 times slower than currentTimeMillis
    Meaning if you do it too often it will have an influence on your calculations.

    Earl Bosch

    May 4, 2017 at 6:19 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: