Calculate remaining time

Started by
4 comments, last by 3Dgonewild 16 years, 8 months ago
Hi! I need your help ..(again) ! My game uses around 500 - 600 * objects *(stick figures / boxes / bonuses etc) per level , and im trying to implement a "functional" loading screen! I've finished the main code , but im having a little problem with remain time and percentage. A test example:


int  objects = cache.getsize();
for PROGRESS =0 to objects
{
Create the object..
//and here's where i need your help..
ShowText("remain time : ",how_to_calculate_remain_time?);
ShowText("Percent done : ",....percentdone....);
}




Can someone please tell me how to calculate the remain time , and the progress(percent) ?
Advertisement
One, you can do a less exact approach of just using the object #'s
so if you have N objects you know you are loading,
percent done is : x / N where x is what object # you are currently loading
time remaining is : time_elapsed / x * (N - x)

If you know the exact data size for the level, you can do a closer to real tally by keeping track of the bytes of data
load, and use that instead of the object number.
Looks like a CPU dependent, quick and dirty way but this can also help.

Save initialization times foreach object type for the first time program executes (say in a text file)

remainingtime = 0

foreach object
remainingtime += GetTypeTimeFromTextFile(object.type)

for progress = 0 to objects
remainingtime -= GetTypeTimeFromTextFile(object.type)
Generally you don't do remaining time beacause it changes all the time based on user load. maybe i'm playing your game and downloading torrents or playing mp3s at the same time. maybe i pause the mp3 in the middle of your loading. That's going to change how much CPU time you use and therefore how much time remains.

The only way you could kind of do it would be to keep a running average of how long it took to load each object; or, perhaps more accurately, how long it to load the last 3-5 objects. Then multiply that by the remaining number of objects.

So you're "remaining time" is going to sometimes go up and sometimes go down (just like the remaining time calculations for downloading stuff from the internet.

For that reason, most people just use a loading bar that fills up as different things happen. There's no actual time displayed there, but the user sees something moving towards and end point and their brain can relatively accurately estimate the remaining time.

-me
Also - don't update the display for every object. That increases load times, since you need to trigger completely unrelated code;

For periodic updates, I use something like this:
template < int Granularity >class Counter{public:  Counter( ) : m_counter(0) {}  inline bool inc() {    return (m_counter++ & ( (1 << Granularity) - 1 ) == 0);  }  inline size_t value() const {    return m_counter;  }private:  size_t m_counter;};


Which you then use like this:
Counter<4> c();   // 1 << 4 = 16, update every 16 objects...for ( ... ) {  // do stuff  if (c.inc()) {    // display progress  }}


This results in very fast run-time test and nice encapsulation of progress logic.

Using granularity of 0 would update on every object. Increasing granularity for such tests becomes more important as the number of updates increases (tens, hundreds of thousands, even millions of updates).

In C++ you also gain benefit from templates, which will likely result in above code being completely embedded in assembly, with no function call overhead, and fast test against constant.

T H A N K S everyone!

I think that this is exactly what i need:
Quote:Original post by KulSeran

percent done is : x / N where x is what object # you are currently loading
time remaining is : time_elapsed / x * (N - x)



But i have a question..
Whats the best way to calculate the time that has passed?

Something like this will work:

int OLD_TIME = static_cast<int>(gettime(null)); while loading...int NOW_ = static_cast<int>(gettime(null));int DIFFERENCE = static_cast<int>( OLD_TIME - NEW_TIME);Remaining_Time =DIFFERENCE/ COUNTER * (TOTAL - COUNTER);


??

This topic is closed to new replies.

Advertisement