Click here to Skip to main content
13,253,159 members (50,660 online)
Rate this:
Please Sign up or sign in to vote.
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 8-Nov-12 1:02am
CPallini 8-Nov-12 6:07am
It depends on the format of your dates. You should give us more details.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
    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; }
    LARGE_INTEGER	m_qwStartTime;
    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)
bool CMyTimer::Start(void)
    // Get performance counter frequency once with first call.
    if (0LL == s_qwPerfFreq.QuadPart)
    return (0LL != s_qwPerfFreq.QuadPart && 
// 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) :

This can be used this way:
CMyTimer Timer(true);
// execute some time consuming code
_tprintf(_T("Code excution took %.5f seconds\n"), Timer.GetElapsedTime());
missak boyajian 8-Nov-12 12:44pm
I am using windows 8 and visual studio 2012
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 12:58pm
So the code will work (it is Windows related and will not work with other OS like Linux).
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.
Jochen Arndt 8-Nov-12 13:28pm
I have pasted the code from VC after checking. I did it again quite now by pasting the above code into VC and it compiles without error.

If you got errors, read the message (it contains the line number). Then check your code at that line. Most errors are just typos.
missak boyajian 8-Nov-12 13:36pm
Yeah Thanks it worked. I copied again and pasted it all over again it worked.
Marcus Kramer 8-Nov-12 14:09pm
+5. Excellent.
Jochen Arndt 8-Nov-12 14:19pm
Thank you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100