Sign in to follow this  

timer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Nope. But there's the clock() function, which should be fine.
Example:

#include <time.h>
void DoSomething()
{
clock_t clockStart = clock();
// Do something here that takes a long time
clock_t clockDuration = clock()-clockStart;

float fTime = ((float)clockDuration) / ((float)CLOCKS_PER_SEC);
printf("It took %f seconds\n",fTime);
}


Share this post


Link to post
Share on other sites
I was just working on updating my timer system when I seen this thread. This is part of my timer system that you can look at and get some ideas from. (The other part just uses STL to handle/manage multiple timers at once)

The data types are as they appear:
P25DI64 = __int64
P25Dvoid = void
P25Dbool = bool
etc.

(Sorry about the formatting)

P25D_Timer.h

//::|ññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| ~ A R I E L P R O D U C T I O N S ~ |ññ|::
//::|ññ| ~ ALL RIGHTS RESERVED ~ |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| ~ PROGRAM DATA ~ /-----/ |ññ|::
//::|ññ| /-----/ | |ññ|::
//::|ññ| PROGRAMMER:: James Dougherty | | | | |ññ|::
//::|ññ| COPYRIGHT :: ©2004-2005 Ariel Productions | | | | |ññ|::
//::|ññ| TYPE :: Phantom Timer System | /---|-/ |ññ|::
//::|ññ| /-----/ |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| ~ SOURCE DATA ~ |ññ|::
//::|ññ| |ññ|::
//::|ññ| FILENAME :: P25D_Timer.h |ññ|::
//::|ññ| VERSION :: 1.0 |ññ|::
//::|ññ| |ññ|::
//::|ññ| HISTORY DATA |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |-N/A |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |ññ|::
//::|ññ| BUGS |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |-N/A |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| This program is Copyright ©2004-2005 Ariel Productions (James Dougherty). This |ññ|::
//::|ññ| program is not in the public domain, therefore, it is not open source and all |ññ|::
//::|ññ| copyrights remain intact. This program constitutes proprietary information and |ññ|::
//::|ññ| trade secrets of Ariel Productions, Ashland, Ohio. This program or any part of |ññ|::
//::|ññ| this program may not be copied, sold, rented, leased, given, assigned, or |ññ|::
//::|ññ| otherwise made available to any other individual or organization without the |ññ|::
//::|ññ| written consent of Ariel Productions. Also, no part of this unpublished work may |ññ|::
//::|ññ| be reproduced, transmitted, or stored in a retrieval system. Nor can this |ññ|::
//::|ññ| program be translated into any human or computer language in any form or by any |ññ|::
//::|ññ| means electronically, mechanically, magnetically, optically, chemically, manual |ññ|::
//::|ññ| or otherwise without the written permission of the copyright owner |ññ|::
//::|ññ| (Ariel Productions). Contact information is listed below for more information. |ññ|::
//::|ññ| |ññ|::
//::|ññ| Ariel Productions |ññ|::
//::|ññ| James Dougherty |ññ|::
//::|ññ| arielproductions@zoominternet.net |ññ|::
//::|ññ| |ññ|::
//::|ññ| Please review "Phantom 25D License.doc" for any updates. |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññ|::

#ifndef P25D_TIMER_H
#define P25D_TIMER_H
#include "..\P25D Definition Files\P25D_Definition.h"

enum P25DTIMERTYPE
{
P25DTMR_CPU = 0x00,
P25DTMR_HIRES = 0x01,
P25DTMR_LORES = 0x02
};

class P25DTIMER
{
private:
P25Dbool m_Stopped;
P25Dint m_TimerType;
P25DI64 m_StartTime;
P25DI64 m_CurrentTime;
P25Dfloat m_ElapsedTime;
P25Dfloat m_AbsoluteTime;

private:
P25Dvoid GetTick(P25Dvoid *Tick);
P25Dfloat GetResolution(P25Dvoid);

public:
P25DTIMER();
virtual ~P25DTIMER();

P25Dvoid InitializeTimer(P25DTIMERTYPE TimerType = P25DTMR_CPU);
P25Dvoid StartTimer(P25Dvoid);
P25Dvoid StopTimer(P25Dvoid);
P25Dvoid UpdateTimer(P25Dvoid);
P25Dvoid AdvanceTimer(P25Dvoid);

public:
P25Dfloat GetAbsoluteTime(P25Dvoid);
P25Dfloat GetElapsedTime(P25Dvoid);
};

#endif //--P25D_TIMER_H







P25D_Timer.cpp

//::|ññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| ~ A R I E L P R O D U C T I O N S ~ |ññ|::
//::|ññ| ~ ALL RIGHTS RESERVED ~ |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| ~ PROGRAM DATA ~ /-----/ |ññ|::
//::|ññ| /-----/ | |ññ|::
//::|ññ| PROGRAMMER:: James Dougherty | | | | |ññ|::
//::|ññ| COPYRIGHT :: ©2004-2005 Ariel Productions | | | | |ññ|::
//::|ññ| TYPE :: Phantom Timer System | /---|-/ |ññ|::
//::|ññ| /-----/ |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| ~ SOURCE DATA ~ |ññ|::
//::|ññ| |ññ|::
//::|ññ| FILENAME :: P25D_Timer.cpp |ññ|::
//::|ññ| VERSION :: 1.0 |ññ|::
//::|ññ| |ññ|::
//::|ññ| HISTORY DATA |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |-N/A |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |ññ|::
//::|ññ| BUGS |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |-N/A |ññ|::
//::|ññ| | |ññ|::
//::|ññ| |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññ| This program is Copyright ©2004-2005 Ariel Productions (James Dougherty). This |ññ|::
//::|ññ| program is not in the public domain, therefore, it is not open source and all |ññ|::
//::|ññ| copyrights remain intact. This program constitutes proprietary information and |ññ|::
//::|ññ| trade secrets of Ariel Productions, Ashland, Ohio. This program or any part of |ññ|::
//::|ññ| this program may not be copied, sold, rented, leased, given, assigned, or |ññ|::
//::|ññ| otherwise made available to any other individual or organization without the |ññ|::
//::|ññ| written consent of Ariel Productions. Also, no part of this unpublished work may |ññ|::
//::|ññ| be reproduced, transmitted, or stored in a retrieval system. Nor can this |ññ|::
//::|ññ| program be translated into any human or computer language in any form or by any |ññ|::
//::|ññ| means electronically, mechanically, magnetically, optically, chemically, manual |ññ|::
//::|ññ| or otherwise without the written permission of the copyright owner |ññ|::
//::|ññ| (Ariel Productions). Contact information is listed below for more information. |ññ|::
//::|ññ| |ññ|::
//::|ññ| Ariel Productions |ññ|::
//::|ññ| James Dougherty |ññ|::
//::|ññ| arielproductions@zoominternet.net |ññ|::
//::|ññ| |ññ|::
//::|ññ| Please review "Phantom 25D License.doc" for any updates. |ññ|::
//::|ññ ---------------------------------------------------------------------------------- ññ|::
//::|ññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññññ|::

#include "P25D_Timer.h"

//------------------------------------------------------------------------------------------------
//--
//--P25DTIMER(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25DTIMER::P25DTIMER() : m_TimerType(P25DTMR_CPU),
m_Stopped(P25Dtrue),
m_AbsoluteTime(0),
m_ElapsedTime(0),
m_CurrentTime(0),
m_StartTime(0)
{
//--
}

//------------------------------------------------------------------------------------------------
//--
//--P25DTIMER(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25DTIMER::~P25DTIMER()
{
//--
}

//------------------------------------------------------------------------------------------------
//--
//--InitializeTimer(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dvoid P25DTIMER::InitializeTimer(P25DTIMERTYPE TimerType)
{
this->m_TimerType = TimerType;
}

//------------------------------------------------------------------------------------------------
//--
//--StartTimer(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dvoid P25DTIMER::StartTimer(P25Dvoid)
{
if(this->m_TimerType == 0x00 || this->m_TimerType == 0x01)
{
GetTick(&this->m_StartTime);
}
else
{
this->m_StartTime = timeGetTime();
}
this->m_Stopped = P25Dfalse;
}

//------------------------------------------------------------------------------------------------
//--
//--StopTimer(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dvoid P25DTIMER::StopTimer(P25Dvoid)
{
this->m_Stopped = P25Dtrue;
}

//------------------------------------------------------------------------------------------------
//--
//--UpdateTimer(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dvoid P25DTIMER::UpdateTimer(P25Dvoid)
{
if(this->m_Stopped)
{
return;
}

if(this->m_TimerType == 0x00 || this->m_TimerType == 0x01)
{
GetTick(&this->m_CurrentTime);
}
else
{
this->m_CurrentTime = timeGetTime();
}
}

//------------------------------------------------------------------------------------------------
//--
//--AdvanceTimer(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dvoid P25DTIMER::AdvanceTimer(P25Dvoid)
{
if(this->m_Stopped)
{
return;
}

if(this->m_TimerType == 0x00 || this->m_TimerType == 0x01)
{
P25DI64 Rate = 0;
GetTick((P25DLI*)&Rate);
this->m_CurrentTime += (P25Dfloat)Rate / 10.0f;
}
else
{
this->m_CurrentTime += 0.1f;
}
}

//------------------------------------------------------------------------------------------------
//--
//--GetTick(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dvoid P25DTIMER::GetTick(P25Dvoid *Tick)
{
if(this->m_TimerType == 0x00)
{
__asm
{
PUSH EDX;
PUSH ECX;
MOV ECX, Tick;
_EMIT 0Fh
_EMIT 31h
MOV [ECX], EAX;
MOV [ECX + 4], EDX;
POP ECX;
POP EDX;
}
}
else if(this->m_TimerType == 0x01)
{
QueryPerformanceCounter((P25DLI*)Tick);
}
}

//------------------------------------------------------------------------------------------------
//--
//--GetResolution(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dfloat P25DTIMER::GetResolution(P25Dvoid)
{
static P25Dfloat Resolution = -1.0f;
if(Resolution == -1.0f)
{
if(this->m_TimerType == 0x00 || this->m_TimerType == 0x01)
{
P25DI64 Rate = 0;
if(QueryPerformanceFrequency((P25DLI*)&Rate))
{
Resolution = (P25Dfloat)Rate;
}
else
{
Resolution = 10000000.0f;
}
}
else
{
Resolution = 1000.0f;
}
}
return Resolution;
}

//------------------------------------------------------------------------------------------------
//--
//--GetCurrentTime(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dfloat P25DTIMER::GetAbsoluteTime(P25Dvoid)
{
P25Dfloat CurrentTick;
if(this->m_Stopped)
{
if(this->m_TimerType == 0x00)
{
return this->m_AbsoluteTime * 0.002f;
}
else
{
return this->m_AbsoluteTime;
}
}

if(this->m_TimerType == 0x00 || this->m_TimerType == 0x01)
{
GetTick(&CurrentTick);
}
else
{
CurrentTick = timeGetTime();
}

this->m_AbsoluteTime = (P25Dfloat)CurrentTick / GetResolution();
if(this->m_TimerType == 0x00)
{
return this->m_AbsoluteTime * 0.002f;
}
else
{
return this->m_AbsoluteTime;
}
}

//------------------------------------------------------------------------------------------------
//--
//--GetElapsedTime(*)
//--Version: 1.0
//--
//------------------------------------------------------------------------------------------------
P25Dfloat P25DTIMER::GetElapsedTime(P25Dvoid)
{
if(this->m_Stopped)
{
if(this->m_TimerType == 0x00)
{
return this->m_ElapsedTime * 0.002f;
}
else
{
return this->m_ElapsedTime;
}
}

this->m_CurrentTime -= this->m_StartTime;
this->m_ElapsedTime = (P25Dfloat)this->m_CurrentTime / GetResolution();

if(this->m_TimerType == 0x00)
{
return this->m_ElapsedTime * 0.002f;
}
else
{
return this->m_ElapsedTime;
}
}







Hope it helps you out.

[EDIT]
I just copied and pasted from my source files. The timer code is not "Top Secret" so use what you like.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this