Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Loop repetition time

    I ran this program:
    Java Code:
    public static void main ( String args[] ) {
    while(true) {
    System.out.println(System.currentTimeMillis());
    }
    }
    Here is what I got for my output (a short section):
    1400639218831
    1400639218832
    1400639218833
    1400639218835
    1400639218836
    1400639218887
    1400639218890
    1400639218891
    1400639218893
    1400639218895
    1400639218947
    1400639218949
    1400639218951
    1400639218953
    1400639218954
    1400639218955
    1400639219005
    1400639219006
    1400639219007
    1400639219007
    1400639219008
    1400639219059
    1400639219059
    1400639219060
    1400639219061
    1400639219062
    1400639219063
    1400639219113
    1400639219114
    1400639219115
    1400639219115
    1400639219116
    Why are there so many skips? Or two of the same number? How does java time each repetition? What's going on here.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop repetition time

    Java doesn't time anything here; it just prints the wall clock time but it is interrupted by other tasks and the os itself (they also want to take turns doing their important stuff).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Loop repetition time

    It's not a fixed update where it prints it out at specific intervals... It just prints it out as fast as it can, sometimes it slows down other times it speeds up

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Loop repetition time

    What operating system did you run it on?

    If you try it on some flavor of windows without special code you'll find the same numbers just repeat a lot and jump up either ~10 or ~15, depending on which computer you run it on. Here is the output on my Windows 7 work machine for example:

    Java Code:
    1400685842750
    1400685842750
    1400685842750
    1400685842750
    <snip>
    1400685842750
    1400685842750
    1400685842750
    1400685842766
    1400685842766
    1400685842766
    1400685842766
    1400685842766
    <snip>
    1400685842766
    1400685842766
    1400685842766
    etc.
    On Mac or Linux you won't see that; I'm wondering if Windows 8 has seen any changes there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Loop repetition time

    I did this on windows 8.

    I'm just a bit confused, because when I did this:
    Java Code:
    while(true) {
    if(System.currentTimeMillis() % 1000 == 0) {
    System.out.println("Second!");
    }
    }
    It printed out "Second!" at the correct interval (sorry, that may need to be a 10000 - I can't find the actual code snippet that I used yesterday).

    So how, when in the first while loop it randomly jumps or repeats numbers, with this new while loop with a print statement, it correctly prints "Second!" every second. It doesn't skip or repeat...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Loop repetition time

    Beef up the test by printing the milli secs with the String.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Loop repetition time

    The % operation seems to take long enough so that double entries are uncommen and it only jumps from time to time so you simply might have been lucky.

    If you check it very carefull, then you will also see missing entries and maybe also double entries.

    Konrad

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Loop repetition time

    Quote Originally Posted by AlexGraal View Post
    I did this on windows 8.
    Thanks, good to know.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop repetition time

    MS Window's wall clock device is crap; it can only handle an accuracy of +- 8 millis; a 1GHz RISC processor could execute a million instructions in a single millisecond and MS manages to ruin it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Loop repetition time

    Quote Originally Posted by JosAH View Post
    MS Window's wall clock device is crap; it can only handle an accuracy of +- 8 millis; a 1GHz RISC processor could execute a million instructions in a single millisecond and MS manages to ruin it ...
    I fear that it is not that easy. In general the cpu runs at a given speed. The operating system does not make a difference (As long as they do not change speed e.g. on a mobile device to reduce the power consumption). So the only question is, what the cpu should do.

    Even if car comparisons are always bad and smelly:
    If you drive a car it is your decision, when you do stops. You can stop every few meters, open the hood and do some checks. And heya great: Your motor is checked so often. great! Or you can simply drive and do the checks whenever you think that you have the time for it. But yes - then people might not be satisfied with the checks done.

    Regarding the clock: You can spend more effort on it. Some interrupt could be used so that it is really set to a correct value. But why is that important? The clock "jumps" a little bit. In a Java application! Maybe on a virtual system!
    Sorry, but who cares?
    If you really care because you need something like that: Then please choose the correct operating system. It is not Windows and it is not a standard linux and of course not a MacOS. Then please take a real time operating system that is able to guarantee timings.
    And then please: Do not use any managed stuff where managing threads (e.g. a garbage collector) can get in your way.

    And just about the "who cares". In virtual environments it is quite normal that the clock jumps. If the host is under load then a vm might encounter small jumps. So if the vm is not executed for 10 ms then there is of course no update on the clock. So for the VM, the clock will jump 10ms. (That is the reason why you should configure the clock of a vm to take the clock data from the host.)

    With kind regards,

    Konrad

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop repetition time

    It's the latency between the moment a certain low level event occurs (a clock tick) and the time it takes to respond to it; a RTOS (Real Time OS) manages to keep that latency constant; not so wth MS Windows: i.e. it stacks the response address (an interrupt handler) and handles it later in time; it's just the design of the thing: it's crappy; Linux manages to keep the latency for clock events below one millisecond and fairly constant even if it isn't marked as a RTOS; I see no reason why MS can't do it ... and b.t.w. clocks don't jump; those crystals are way too stupid for that; they skew ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Loop repetition time

    Quote Originally Posted by JosAH View Post
    It's the latency between the moment a certain low level event occurs (a clock tick) and the time it takes to respond to it; a RTOS (Real Time OS) manages to keep that latency constant; not so wth MS Windows: i.e. it stacks the response address (an interrupt handler) and handles it later in time; it's just the design of the thing: it's crappy; Linux manages to keep the latency for clock events below one millisecond and fairly constant even if it isn't marked as a RTOS; I see no reason why MS can't do it ... and b.t.w. clocks don't jump; those crystals are way too stupid for that; they skew ;-)

    kind regards,

    Jos
    Correct; you can influence it though; Windows provides an API method to (system wide) change the granularity so you can actually set it to 1ms. It just so happens that in Java you can indirectly cause that to be called, like this:

    Java Code:
    public static void main ( String args[] ) {
    	
    	Thread dummy = new Thread() {
    	  
    	  public void run() {
    	    try{
    		   Thread.sleep(Long.MAX_VALUE);
    		} catch(Throwable t){
    		}
    	  }
    	};
    	
    	dummy.setDaemon(true);
    	dummy.start();
    	
    	while(true) {
    		System.out.println(System.currentTimeMillis());
    	}
     }
    So I start a thread which then immediately goes to sleep basically forever. As long as a thread is asleep, the Windows JVM keeps the granularity of the clock at 1ms.

    When I run that code on the same system that produces the +15ms jump posted earlier, I now get this:

    Java Code:
    1400750382497
    1400750382497
    1400750382497
    1400750382497
    1400750382497
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382498
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382499
    1400750382500
    1400750382500
    1400750382500
    1400750382500
    1400750382500
    1400750382500
    1400750382500
    1400750382500
    1400750382500
    Magic.

    What I don't understand is: why does the OP not get this result; where the time goes up 1ms and the same number repeats a few times. An application which does almost nothing is going to do that almost nothing multiple times in the same millisecond. Unless the CPU is dead slow or under heavy load already as mentioned before.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop repetition time

    I don't understand why a sleeping (user defined) thread would make any difference; when the Sun/Oracle JVM starts up, it starts up at least a few threads that do the same (i.e. fall asleep immediately) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Loop repetition time

    I guess those threads are "different" ? That's another puzzle indeed, never thought of that.

    Thanks dude, now I'm going to lie awake wondering.

    EDIT: well one thing I can come up with is that those threads don't go to sleep-sleep but they in stead go into a waiting state through some locking mechanism.
    Last edited by gimbal2; 05-22-2014 at 12:47 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop repetition time

    Check it with 'jconsole'; it's a tool in the JDK and it can monitor threads in a JVM.

    <typedeetype, fiddle, fiddle>
    You might be right; none of those threads are sleeping; they're either waiting, or parked (whatever that means) or blocked ...
    </typedeetype, fiddle, fiddle>


    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Loop repetition time

    Quote Originally Posted by JosAH View Post
    Check it with 'jconsole';
    Yes or the Eclipse debugging view.

    it's a tool in the JDK and it can monitor threads in a JVM.
    Curses, the I'll-educate-you-fish-slap. Alright, I'll take it like a real man; with a 1200 baud modem.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Loop repetition time

    If you do it like this, you get all 0's with the millisecond time. So probably better to use System.nanoTime(). This still has some processing in the loop but it is probably more accurate than printing them out inside the loop.
    Java Code:
         public static void main(String[] args) {
          long [] timeVals = new long[5000];
          int index = 0;
          try {
             while (true) {
                timeVals[index++] = System.nanoTime();
             }
          }catch (IndexOutOfBoundsException ioe) {
             for (int j = 1; j < index-1; j++) {
                System.out.println(timeVals[j] - timeVals[j-1]);
             }
          }|
    Edit: I missed it, during the 5000 count there was a single delta of 15 milliseconds (using currentTimeMillis()).

    Regards,
    Jim
    Last edited by jim829; 05-22-2014 at 02:48 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Loop repetition time

    Uh, wow, thank you for all of the responses.

    I know much more, but I at the same time, the expanded knowledge is causing more questions. What gimbal said is exactly what I was wondering in the beginning.

    I assume that when I have my program do a lot of work, the jvm will get overwhelmed, possibly skipping a few numbers. But when I run literally 5 lines of code, just to print out "second" everytime the System.currentTimeMillis() call returns a number ending in 0000, I would expect it to now be overwhelmed - to actually get repeat number. But doesn't that mean that I should have "Second" printed out in bursts?

    Maybe if somebody could briefly describe the connection between hardware and software that makes an actual timing system possible, I would get further insights.

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Loop repetition time

    I don't understand it either. You SHOULD get the same results as me. Its almost like fetching the clock time is wickedly slow on your system.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Loop repetition time

    Quote Originally Posted by gimbal2 View Post
    I don't understand it either. You SHOULD get the same results as me. Its almost like fetching the clock time is wickedly slow on your system.
    Quote Originally Posted by AlexGraal View Post
    I did this on windows 8.
    :)
    I suspect there's a lot more rubbish going on in the background of a W8 machine to even a W7 one.
    Mine seems to burble away to itself for no apparent reason.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. time complexity about 3^n using nested for loop
    By hjxlpp in forum New To Java
    Replies: 1
    Last Post: 06-25-2013, 10:26 AM
  2. declaring a different array each time i loop
    By MichaelT in forum New To Java
    Replies: 13
    Last Post: 06-09-2011, 03:13 AM
  3. Time complexity - foor loop
    By hawaiifiver in forum New To Java
    Replies: 5
    Last Post: 02-05-2011, 05:06 PM
  4. Nested Loop help -- concerning image repetition
    By Submerged in forum Java Applets
    Replies: 0
    Last Post: 03-05-2010, 04:57 AM
  5. while Loop (Repetition) structure.
    By gapeach97 in forum New To Java
    Replies: 9
    Last Post: 12-04-2008, 12:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •