Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ['while(!destroyed)' loop behaving abnormal?] *solved*  (Read 605 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-01-13 23:09:58 »

Well, last night before I went to sleep I was programming the majority of that day working on a program that would:

A: Record the users screen every 21 - 33.3 milliseconds.
B: After recording, store the captured frames, than start a GUI that will shuffle through the pictures giving a feeling of smooth motion. (play/pause buttons etc)

My problem while programming is that half the time while I'm programming a project, after I've turned it into something nice/useable, I've as some might call it 'hard-coded' it, figured out where it starts/stops etc programmed the program to run smoothly if that makes sense o.O.

Here is my question: Why is example 2 taking 3 seconds+ to complete 1 loop, where in example 1 it executes 1 loop in about 0-16ms?
The only difference between the two examples, it that example 2 calls 'System.out.println' before starting the loop.

Example 1:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public Launcher() {
      int loops= 0;
      elapsed = System.currentTimeMillis();
      while(!destroyed){
         if (loops < 2147000000) {
            loops++;
         } else {
            System.out.println("[BreakPoint @ "+loops+" loops, finished in: "+(System.currentTimeMillis() - elapsed)+"ms]");
            destroyed = true;
         }
      }
      System.out.println("[BreakPoint: After While Statement]");
   }

Example 1 returns:
1  
2  
[BreakPoint @ 2147000000 loops, finished in: 0ms]
[BreakPoint: After While Statement]

Example 2:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   public Launcher() {
      int loops= 0;
      System.out.println("[StartPoint @ "+loops+" loops]");
      elapsed = System.currentTimeMillis();
      while(!destroyed){
         if (loops < 2147000000) {
            loops++;
         } else {
            System.out.println("[BreakPoint @ "+loops+" loops, finished in: "+(System.currentTimeMillis() - elapsed)+"ms]");
            destroyed = true;
         }
      }
      System.out.println("[BreakPoint: After While Statement]");
   }

Example 2 Returns:
1  
2  
3  
[StartPoint @ 0 loops]
[BreakPoint @ 2147000000 loops, finished in: 2996ms]
[BreakPoint: After While Statement]


Hope somebody has some feedback/input, going to be depending on consistency ^__^.

EDIT
: I never knew this, I removed the Integer variable 'loops' out of the Constructor, and added it's decleration in the main class as a variable instead, just by doing that it caused the interval rate of which the loop performs at to hop from 0-16ms to 152-172ms?
Nothing's different, just where the variable was declared D:

Offline pitbuller
« Reply #1 - Posted 2013-01-13 23:46:37 »

If JIT just optimize first loop out but not second because of bigger size of function. Inlining have byte code size cap so maybe this is just anomaly cause by JIT optimizer.
Offline GabrielBailey74
« Reply #2 - Posted 2013-01-13 23:56:13 »

JIT?, I've never heard of that, what is it? Shocked

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2013-01-14 00:47:30 »

JIT?, I've never heard of that, what is it? Shocked

troll ? or have you never read a book about java ever ?

Offline GabrielBailey74
« Reply #4 - Posted 2013-01-14 00:53:25 »

I've literally only read 1 book on java, and that was after a few years of programming Java.
Even in that book relating to java, never mentioned 'JIT'?

If I ask what 'JIT' (A word I've never heard of) is, I'm a troll? Cranky

I just wrote that 10 lines of code this morning, didn't understand why 'System.out.println("");' was delaying it for 3+ seconds.

Offline Best Username Ever

Junior Member





« Reply #5 - Posted 2013-01-14 01:03:21 »

Is this a busy loop for a delay function? Stare
Offline Best Username Ever

Junior Member





« Reply #6 - Posted 2013-01-14 01:14:04 »

I never knew this, I removed the Integer variable 'loops' out of the Constructor, and added it's decleration in the main class as a variable instead, just by doing that it caused the interval rate of which the loop performs at to hop from 0-16ms to 152-172ms?
Nothing's different, just where the variable was declared D:

That is easily explained. Local variables cannot be seen from anywhere other than the function you are currently in, so there is no need to make sure changes are visible to other code. Local variables can be stored on the stack or in a CPU register. Register reads/writes are as fast as you can get. Field variables may need to be written to RAM, meaning
loops++
would read and write a value every time it was executed. A register could be incremented in one CPU instruction and with no memory delays for reading or writing, since the data is already at hand and the computer knows nothing else changed its value.
Offline GabrielBailey74
« Reply #7 - Posted 2013-01-14 01:16:30 »

@Best Username Ever:
Is this a busy loop for a delay function? Stare

Lol, no Tongue.
That is a snippet from Launcher.java (Simply some variables and the constructor displayed above in the examples).

This doesn't have too much to do with my 'Java Screen Recorder / Player'.
I only wrote that code in the examples above this morning so that I could debug a 'while(condition == true)' loop, and see when it ended etc, and in the process of doing that i discovered the 'opening post' issue.

@Best Username Ever:
That is easily explained. Local variables cannot be seen from anywhere other than the function you are currently in, so there is no need to make sure changes are visible to other code. Local variables can be stored on the stack or in a CPU register. Register reads/writes are as fast as you can get. Field variables may need to be written to RAM, meaning
loops++
would read and write a value every time it was executed. A register could be incremented in one CPU instruction and with no memory delays for reading or writing, since the data is already at hand and the computer knows nothing else changed its value.

That's badass lol.
So I haven't been the optimizer I thought I was. Stare
Thanks mate, noted.

Offline Best Username Ever

Junior Member





« Reply #8 - Posted 2013-01-14 01:46:32 »

Your loop code can be expressed as:

1 goto 8 if destroyed
2 goto 5 if loops >= 2147000000
3 increment loops
4 goto 1
5 sop blah blah blah
6 destroyed = true
7 goto 1


Which can be rewritten as:

1 goto 8 if destroyed
2 goto 5 if loops >= 2147000000
3 increment loops
4 goto 2
5 sop blah blah blah
6 destroyed = true
7 goto 8


Or:

1 goto 8 if destroyed
2 loops = 2147000000
3 goto 5
4 ...
5 sop blah blah blah
6 destroyed = true
7 goto 8


Or:

1 goto 5 if destroyed
2 loops = 2147000000
3 sop blah blah blah
4 destroyed = true


Which is the same as
1  
2  
3  
4  
5  
6  
if(!destroyed)
{
  loops = 2147000000;
  System.out.println(...);
  destroyed = true;
}


Both do the same amount of work, but the loop does it less efficiently. The compiler notices it and replaces the original code with more efficient code. It does not automatically recognize every possible improvement and might take a long time if it could, so it might pass over certain blocks of code and assumes the programmers were smart enough to figure it out themselves. A just in time (JIT) compiler may make optimizations on the fly, so its possible that if you called that function a few more times and the compiler decided it was worth the investment to try improving that code even if it did not do so ahead of time (AOT). Three times is a magic number in Hotspot, so both versions might run in 0ms if you tested it enough times in the same run, but you should not rely on optimization of any form for changing the structure of your program.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!