# Game loop delta, factoring execution time?

Was thinking this morning, I 99.9% of the time, only see a main game loop, looking like this, just as much so where the delta is passed into update():

while running
getDeltaTime()
...
f(deltatime)
---


Now the question I was thinking to myself was what if between getDeltaTime() and f(deltaTime), the code in the middle took 1 second (large value to be easy). That means f(deltaTime) would have moved whatever object less distance than it was supposed to. This would of course be solved by grabbing the delta time directly before it is used.

Should this be a factor to be concerned with in any case?

deltatime is the time between two consecutive calls to getDeltaTime(). If a particular frame takes very long, that will show in the next iteration, and the model will be updated accordingly.

That being said, you're right: you should call f(..) straight away. Other code after it. Delays introduced by that extra code, as well as the time it takes to update the model itself, will show in the delta of the next frame.

• That makes sense, not something I have considered before, but I will keep immediate calling in mind. – Rick Taemen May 30 '13 at 9:56

Yes, it is a factor. Some engines take this into account in some cases. For example AndEngine and its LimitedFPSEngine:

@Override
public void onUpdate(final long pNanosecondsElapsed) throws InterruptedException {
final long preferredFrameLengthNanoseconds = this.mPreferredFrameLengthNanoseconds;
final long deltaFrameLengthNanoseconds = preferredFrameLengthNanoseconds - pNanosecondsElapsed;

if (deltaFrameLengthNanoseconds <= 0) {
super.onUpdate(pNanosecondsElapsed);
} else {
final int sleepTimeMilliseconds = (int) (deltaFrameLengthNanoseconds / TimeConstants.NANOSECONDS_PER_MILLISECOND);