Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is tight looping bad?

Tags:

java

Is tight looping in a program bad?

I have an application that has two threads for a game-physics simulator. An updateGame thread and a render thread. The render thread is throttled by causing the thread to sleep for some milliseconds (to achieve the frame-rate I want) and the updateGame thread (that updates my in game objects positions based off some physics equations) was previously throttled by a 10 millisecond sleep.

However, I recently unthrottled the updateGame thread and the simulation of my objects movement seems to be significantly more realistic now that I have taken out that 10ms sleep. Is it bad to hot loop or have a tight loop?

private class UpdateTask implements Runnable
{
    private long previousTime = System.currentTimeMillis();
    private long currentTime = previousTime;
    private long elapsedTime;


    public void run()
    {
        while(true)
        {
        currentTime = System.currentTimeMillis();
        elapsedTime = (currentTime - previousTime); // elapsed time in seconds


        updateGame(elapsedTime / 1000f);

            try {
                Thread.currentThread().sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        previousTime = currentTime;
        }
    }
}

In this example I'm just sleeping for 1ms (and from my understanding with how millisecond accuracy and the sleep function works this is probably more like 5-10ms. If I sleep for any more than this it starts to have impacts on the accuracy of my collision detection and physics model.

Is it a bad practice to have tight loops or loops with 1ms sleeps in them? Is there something else I should do instead?

like image 949
mmcdole Avatar asked Dec 12 '08 03:12

mmcdole


People also ask

What does tight loop mean?

Noun. tight loop (plural tight loops) (programming) A loop which contains few instructions and iterates many times. (programming) Such a loop which heavily uses I/O or processing resources, failing to adequately share them with other programs running in the operating system.

When a process repeatedly checks condition in a tight loop?

In computer science and software engineering, busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available.

What is a hot loop code?

A “hot loop” (usually, I think, called a “tight loop”) is a loop that does a lot of work for a relatively large amount of time all at once. Things like “looping through a list of every building an Manhattan and getting the average price over two decades.”


2 Answers

I read a really great post about efficiently and effectively executing physics calculations loop: Fix Your Timestep!

When a game is running that is usually the main application that the user cares about so tight looping is not that big of a deal. What you really should do though schedule your updates. You should know how long -- at your target framerate -- that your frame has to execute. You should measure the time that your frame took and only sleep for the time that your frame took minus that known frame time. That way your system will lock into a frame rate and not vary with the amount of time that your frame takes to render.

Another thing is that I don't believe that Thread.sleep has a very good resolution, well over 5 milliseconds, you may want to look for a more accurate timer available for Java.

like image 167
joshperry Avatar answered Oct 01 '22 09:10

joshperry


It's only "bad" if it has an adverse impact on something else in your system. Rather than sleeping for 1ms, you might block on a condition that warrants updating, with a minimum of 1ms. That way you'll always sleep for at least 1ms, and longer if there's nothing to do.

like image 44
Adam Liss Avatar answered Oct 01 '22 10:10

Adam Liss