Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding System.out.println() slows down execution (a lot)?

I have a while loop with a switch statement:

while(true) {
        switch(state) {
        case LOADING :
            //THIS IS THE IMPORTANT PART
            //view loading screen (already set by default)
            contentPane.repaint();
            if(tick == 400000) {
                //state = GameState.MENU;
                System.out.println("Reached " + gameTick);
            }
            break;
        case MENU :
            //view menu
            break;
        //some other cases without content, left them out here
        }
        tick++;
        if(tick < 400000) {
                System.out.println(tick);
        }
        if(tick == Long.MAX_VALUE) {
            tick = 0;
        }
    }

Now this code executes just fine, it shows the loading screen (which has moving dots on it for as long as it's repaint is repeatedly called, so I know exactly when it stops), and the output counts from 1 to 400000 and on that number prints

399998
399999
Reached 400000    

(last 3 lines of output)

The application goes fullscreen, and when I alt+tab out, the counter is usually somewhere around 130K, and I watch it move to 400K.

However, if I remove the if statement that prints this number:

if(tick < 400000) {
    System.out.println(tick);
}

The loading screen never moves, and when I alt + tab out, the 400K is already reached.

Also curious is that the loading screen has three 'appearance changes', one at 100 calls of it's paintComponent method, one at 200 calls and one at 300 calls, which resets the counter to 0. So basically, every 100 ticks it's supposed to have an appearance change. In the first case, with the if-statement, which has longer execution time, I see the changes but by far not as frequently as I'd expect. In the second case, I don't see them at all (I can imagine they'd happen too fast).

So my question is, what creates this quite big difference in execution time, and what causes the difference in the amount of times the paintComponent method seems to be called, and the 400.000 times the loop iterates?

All ideas appreceated.

like image 815
Mark Tielemans Avatar asked Dec 26 '22 16:12

Mark Tielemans


2 Answers

Writing to the console, especially the MS-DOS console, is very very slow. You should try to keep the number of lines you write to the console to a minimum. If you have to write lots of data I suggest you write it to a file as it can be significantly faster.

I assume this is done in another thread and you are not tying up the GUI thread.

like image 114
Peter Lawrey Avatar answered Jan 09 '23 01:01

Peter Lawrey


The repaint() method does not repaint the component immediately, just signals the AWT/Swing system to repaint the component. So if you call repaint() fast 10 times, there is a good chance it will only be repainted once.

Now if you look at your loop, it does nothing that would take long (note that repaint() does not repaints). If you add a System.out.println() call in the loop, it will significantly increase the work to be done inside your loop.

like image 22
icza Avatar answered Jan 09 '23 03:01

icza