how to calculate the real fps from within a loop

fps and fps_real are only updated once per step and cannot be used to check during a for or a while loop if it is hampering performance.

if i wanted to check fps_real, from within a loop, in order to issue a break command, how would i go about calculating this number?

thank you.
 
Is this what you need? If it only updates once per step, just check it at the start.
Code:
var _fps_real = fps_real;
for (var i=0; i<1000; i++) {
    var _check = _fps_real > 30;
}
 

Relic

Member
Fps literally is the number of frames per second. Until a loop is finished, no frames will have finished so You can’t measure the performance of a single while loop taking a long time.

You may have luck with delta_time to find the time passed since the last step.

There is a reason why you want to do this at runtime right? The debugging profiler will give you better information about what is taking a long time to process each step.
 
I mean alternatively, you could break the loop after so many steps, if you're worried it might take too long to get through.
Code:
var _finished = false,
    _threshold = 1000,
    i = 0;
while !_finished {
    // Do your strenuous code here, e.g. looping through data strucs.
 
    // Break out if over the threshold.
    if i>_threshold break;
 
    i++;
}
 
i attempted to make use of delta time and, while inside of a loop, delta time does not seem to update.

having a threshold to kick if the loop repeats too many times does work, but its based on an arbitrary number, might you know of a way to optimize the 1000, for instance in relation to fps_real or delta_time, so that it would cut off before the loop drags the fps too low and causes lag?

thank you.
 

TsukaYuriko

☄️
Forum Staff
Moderator
You may have more luck using time as your metric, as FPS is probably not what you're looking for (it is measured per second - what you are trying to measure is a fraction of a second). Take a look at current_time - store it in a variable before you start the loop, then compare against it inside of the loop.
 
So, we're working with iteration over a data-set that doesn't seem to particularly mind if you break out of it before it's completed. What is the result of the fully performed task? Is it for visual gain, are we loading the scene for the user, or what? I only ask because if it's non-essential... i.e., nothing is strictly relying on it to be 100% completed before-hand, then can you relegate the task to occur over several frames? Instead of looping, you could start the process at the point you desire, and let the player keep playing the game while the loading occurs behind it. You could be especially clever and do the loading during the menu screen, a cinematic, or a cut-scene. If you did it like this, you could actually put fps_real to good use. If the fps drops too low during the background processing, break the sequence (or drop the graphics... idk, some context would be good).
 
Top