currentTimeMillis not updating frequently

edited October 2013 in Questions about Code

On my PC the milliseconds value displayed by this code only updates every 2 or 3 minutes, and not continuously. Any suggestions as to why? Thanks, Andrew

import java.lang.System;

void setup() {
  size(400, 200);
}

void draw() {
  background(255);
  fill(0);
  text(System.currentTimeMillis(),20,40);
}

Answers

  • edited October 2013

    The easiest option would be to use Processing's built in method millis(). But if you have to use native Java for some reason, this should answer your question:

    If you are interested in measuring absolute time then always use System.currentTimeMillis(). Be aware that its resolution may be quite coarse (though this is rarely an issue for absolute times.)

    If you are interested in measuring/calculating elapsed time, then always use System.nanoTime(). On most systems it will give a resolution on the order of microseconds. Be aware though, this call can also take microseconds to execute on some platforms.

    In other words, System.currentTimeMillis() is used for precise dates and hours but is refreshed less frequently (the rate of this refresh depends on the OS).

    System.nanoTime() is good for elapsed times for but it is more CPU intensive.

    Source: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks

  • edited October 2013

    Hmm... My answer might be off the track as this is what the implementation of millis() looks like:

     public int millis() {
        return (int) (System.currentTimeMillis() - millisOffset);
      }
    
  • edited October 2013 Answer ✓

    println(System.currentTimeMillis()); works properly so it points to a wrong dynamic type casting from long to String.

    Let's try this:

      String ms = Long.toString(System.currentTimeMillis());
      text(ms,20,40);
    

    Bingo!

  • edited October 2013

    Processing isn't fond for all of the 8 Java's primitive types! Data-types byte, short, long & double aren't integrated well! :-O
    Main 1s are int & float. While boolean & char are also tolerable by it. 8-|

  • Yes. Bingo! Not sure I understand the explanation... something about data type conflicts between java and processing ... but thanks guys!

  • edited October 2013

    Hi Andrew.

    I'm glad it was helpful. Please accept an answer so this thread gets cleared from the "unanswered" list.

    Have a nice day :)

Sign In or Register to comment.