Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey. I want to calculate the running time of the code.
Lets say i have 2 dates i want to calculate the difference in milliseconds or microseconds.

What is the best way to do it?
Posted
Comments
CPallini 8-Nov-12 6:07am    
It depends on the format of your dates. You should give us more details.

1 solution

You did not say which operating system you are using. With Windows, the performance counter is the best choice.

Here is a small class using the performance counter that can be used to measure code execution time:

// MyTimer.h
class CMyTimer
{
public:
    CMyTimer(bool bStart = false);
    
    bool		Start();
    double		GetElapsedTime() const;
    inline DWORD 	GetElapsedTimeMS() const
    { return static_cast<DWORD>(1E3 * GetElapsedTime()); }
    inline void		Stop() { m_qwStartTime.QuadPart = 0LL; }
    inline bool		IsStarted() const { return m_qwStartTime.QuadPart != 0LL; }
    inline LONGLONG	GetStartTime() const { return m_qwStartTime.QuadPart; }
    inline LONGLONG	GetFrequency() const { return s_qwPerfFreq.QuadPart; }
protected:
    LARGE_INTEGER	m_qwStartTime;
private:
    static LARGE_INTEGER s_qwPerfFreq;
};

// MyTimer.cpp
#include "MyTimer.h"

/*static*/ LARGE_INTEGER CMyTimer::s_qwPerfFreq = { 0 };

CMyTimer::CMyTimer(bool bStart /*= false*/)
{
    m_qwStartTime.QuadPart = 0LL;
    if (bStart)
        Start();
}

bool CMyTimer::Start(void)
{
    // Get performance counter frequency once with first call.
    if (0LL == s_qwPerfFreq.QuadPart)
    	::QueryPerformanceFrequency(&s_qwPerfFreq);
    return (0LL != s_qwPerfFreq.QuadPart && 
        ::QueryPerformanceCounter(&m_qwStartTime));
}

// Get elapsed time in seconds.
double CMyTimer::GetElapsedTime() const
{
    LARGE_INTEGER qwEndTime;
    return (IsStarted() && ::QueryPerformanceCounter(&qwEndTime)) ?
        static_cast<double>(qwEndTime.QuadPart - m_qwStartTime.QuadPart) / 
        static_cast<double>(s_qwPerfFreq.QuadPart) :
        0.0;
}

This can be used this way:
CMyTimer Timer(true);
// execute some time consuming code
_tprintf(_T("Code excution took %.5f seconds\n"), Timer.GetElapsedTime());
 
Share this answer
 
Comments
missak boyajian 8-Nov-12 12:44pm    
I am using windows 8 and visual studio 2012
Jochen Arndt 8-Nov-12 12:58pm    
So the code will work (it is Windows related and will not work with other OS like Linux).
missak boyajian 8-Nov-12 12:55pm    
Hey. I'm having some errors.
1. DWORD is undefined.
2.LARGE_INTEGER is undefined.
3.m_qwStartTime is undefined.
And others...What should I do?
Jochen Arndt 8-Nov-12 13:02pm    
The header file windows.h must be included. I assumed that it is already included.
missak boyajian 8-Nov-12 13:11pm    
Hey.I included the header file. But in MyTimer.cpp its giving 2 errors I dont know why?
1.m_qwStartTime undefined.
2.IsStarted() undefined.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900