Jump to content

  • Log In with Google      Sign In   
  • Create Account

Compute how long it takes todo something


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Anddos   Members   -  Reputation: 516

Like
0Likes
Like

Posted 19 January 2013 - 06:46 AM

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.


:)

Sponsor:

#2 minibutmany   Members   -  Reputation: 1679

Like
0Likes
Like

Posted 19 January 2013 - 10:08 AM

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.


Stay gold, Pony Boy.

#3 Servant of the Lord   Crossbones+   -  Reputation: 21030

Like
2Likes
Like

Posted 19 January 2013 - 11:23 AM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 rip-off   Moderators   -  Reputation: 8727

Like
1Likes
Like

Posted 19 January 2013 - 12:20 PM

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.



#5 Servant of the Lord   Crossbones+   -  Reputation: 21030

Like
0Likes
Like

Posted 19 January 2013 - 01:09 PM

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


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#6 Anddos   Members   -  Reputation: 516

Like
0Likes
Like

Posted 19 January 2013 - 06:00 PM

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?


:)

#7 Servant of the Lord   Crossbones+   -  Reputation: 21030

Like
0Likes
Like

Posted 19 January 2013 - 06:11 PM

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;

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#8 TheChubu   Crossbones+   -  Reputation: 4766

Like
1Likes
Like

Posted 19 January 2013 - 07:50 PM

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


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#9 Anddos   Members   -  Reputation: 516

Like
0Likes
Like

Posted 25 January 2013 - 08:04 AM

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?


:)

#10 Anddos   Members   -  Reputation: 516

Like
0Likes
Like

Posted 25 January 2013 - 08:43 AM

this is exactly what i want

 

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

DWORD WINAPI test_thread(LPVOID Param)
{
    MessageBox(NULL,"test_thread started ","",0);

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

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

    MessageBox(NULL,"test_thread over","",0);

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE test_thread_handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)test_thread,NULL,NULL,NULL);
    WaitForSingleObject(test_thread_handle,INFINITE);
    return 0;
}

:)

#11 Servant of the Lord   Crossbones+   -  Reputation: 21030

Like
0Likes
Like

Posted 25 January 2013 - 12:27 PM

If you want to test how many seconds it ran for, you can do this:
 
DWORD WINAPI test_thread(LPVOID Param)
{
    MessageBox(NULL,"test_thread started ","",0);

    time_t startTime = time();

    for(int i = 0; i < 99999999999; i++)
    {
        //WARNING! If you don't have anything in this loop, your compiler might optimize the entire loop into non-existence.
    }
     
    time_t endTime = time();
    time_t numSeconds = (endTime - startTime);

    //numSeconds is the number of seconds. You could cast it to unsigned int or int.
    
    MessageBox(NULL,"test_thread over","",0);

    return 0;
}
No, time() is not "high performance". time() returns seconds. You can use the RunTime() function I posted above for millaseconds (1/1000 of a second), which is still not considered "high performance" but is perfectly fine for most uses and what I usually use. When people say 'high performance' they usually mean microseconds (1/1000000 of a second).

There is no cross-platform way to get guaranteed microsecond performance, but C++11 has a few classes for getting microseconds if it's available, and millaseconds if it's not. Win32 also has some microsecond precision timers, if I recall correctly, but they are Windows-only.

Here is the same test as above, but with millasecond performance, if you need it:
DWORD WINAPI test_thread(LPVOID Param)
{
    MessageBox(NULL,"test_thread started ","",0);

    unsigned int startTime = RunTime();

    for(int i = 0; i < 99999999999; i++)
    {
        //WARNING! If you don't have anything in this loop, your compiler might optimize the entire loop into non-existence.
    }
     
    unsigned int endTime = RunTime();
    unsigned int millaseconds = (endTime - startTime);

    //Some conversion examples:
    unsigned int seconds = (millaseconds / 1000); //1000 millaseconds in a second.
    millaseconds %= 1000;
    unsigned int minutes = (seconds / 60); //60 seconds in a minute.
    seconds %= 60;
    unsigned int hours = (minutes / 60); //60 minutes in an hour.
    minutes %= 60;

    //Example: "It took: 0 hours, 2 minutes, 24 seconds, and 456 millaseconds."
    std::cout << "It took: " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds, and " << millaseconds << " millaseconds." << std::endl;
    
    MessageBox(NULL,"test_thread over","",0);

    return 0;
}

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#12 Anddos   Members   -  Reputation: 516

Like
0Likes
Like

Posted 26 January 2013 - 04:58 AM

wow that us fantastic information mate


:)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS