Public Group

# Compute how long it takes todo something

This topic is 2147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the best way to compute how long it takes todo something in seconds,minutes,hours, etc compute how long it takes to complete a level , do you use WM_TIMER and win32 functions or is there something else , thanks for reading.

##### Share on other sites

In order to calculate how much time has passed, you can use the C++ method, time() then perform the action that you want to be timed, then call time() again and subtract to get the amount of time that has passed.

##### Share on other sites

time() returns seconds, not millaseconds, so it'd give a very inaccurate measurement.

Using millaseconds give fairly accurate measurements for most needs. You can use these to get the number of millaseconds past since your program began:

#include <ctime>

//The time, in seconds, since the program started. (1.5f = one and a half seconds)
Seconds RunTimeInSeconds()
{
//CPU "ticks" since the program started.
clock_t programTickCount = clock();

//Convert from ticks to seconds.
float seconds = float(programTickCount) / CLOCKS_PER_SEC;

return seconds;
}

//Same as above, but in millaseconds as an integer. (1500 = one and a half seconds)
Milliseconds RunTime()
{
//CPU "ticks" since the program started.
clock_t programTickCount = clock();

//Conversion rate between ticks and millaseconds.
float msMultiplyer = 1000.0f / CLOCKS_PER_SEC;

//Convert from ticks to seconds.
int millaSeconds = (programTickCount * msMultiplyer);

return millaSeconds;
}

Using microseconds gives ever more accuracy, however there are not any standard microsecond timers in C++, so you have to use third party timers, unless you are using the new C++11 standard's <chrono> library.

##### Share on other sites

The OP probably does not need millisecond accuracy to measure how long the user took to complete a level, let alone microseconds! The time() function should suffice.

If you were measuring the user's reaction time, or measuring how long your program was taking to perform an action, then the higher resolution functions would be preferred.

##### Share on other sites

I completely skimmed past the 'how long it takes to complete a level' part. Whoops.

##### Share on other sites
The OP probably does not need millisecond accuracy to measure how long the user took to complete a level, let alone microseconds! The time() function should suffice.

If you were measuring the user's reaction time, or measuring how long your program was taking to perform an action, then the higher resolution functions would be preferred.

yeh ive been told that, would those functions calculate the same way you calculate the fps , would it also go in the msg loop or in the function thats doing the task?

##### Share on other sites
It'd go something like this:
//When the level begins:
unsigned int startTime = (unsigned int)time(); //Get current clock time. Example: 75400000

//When the level is completed:
unsigned int endTime = (unsigned int)time(); //Get current clock time. Example: 75400127
unsigned int timeItTook = (endTime - startTime); //Example: 75400127 - 75400000 = It took 127 seconds.

You can then do something like:
unsigned int seconds = (endTime - startTime);

unsigned int minutes = (seconds  / 60);
seconds %= 60;

std::cout << "It took you " << minutes << " minutes, and " << seconds << " seconds." << std::endl;

##### Share on other sites
The OP probably does not need millisecond accuracy to measure how long the user took to complete a level, let alone microseconds!

You have to understand, it is a very competitive game :P

##### Share on other sites

It'd go something like this:

//When the level begins:
unsigned int startTime = (unsigned int)time(); //Get current clock time. Example: 75400000

//When the level is completed:
unsigned int endTime = (unsigned int)time(); //Get current clock time. Example: 75400127
unsigned int timeItTook = (endTime - startTime); //Example: 75400127 - 75400000 = It took 127 seconds.

You can then do something like:
unsigned int seconds = (endTime - startTime);

unsigned int minutes = (seconds  / 60);
seconds %= 60;

std::cout << "It took you " << minutes << " minutes, and " << seconds << " seconds." << std::endl;

would that be using the high performance counter?

##### Share on other sites

this is exactly what i want

#include "stdafx.h"
#include <Windows.h>

{

for(int i = 0; i < 99999999999; i++)
{

}
//i would like to work out how many seconds that loop ran for

return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}


1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013372
×