6

I wonder what is the best way to measure the execution time of some code in C++. Is there any built in Stopwatch (.Net) alike class? I am developing C++ on VS2010. How (if) can i use the .Net libraries inside my C++ project?? Thank you In advance.

tropicana
  • 1,403
  • 2
  • 19
  • 27

4 Answers4

3

AFAIK C++ hasn't got an standard class like the Stopwatch in .NET.

http://cplus.about.com/od/howtodothingsi2/a/timing.htm is an example for a high resolution timer on the windows platform.

A platform independent implementation for such timers is: http://www.boost.org/libs/timer/doc/index.html

HTH

Daniel James
  • 3,899
  • 22
  • 30
paiden
  • 887
  • 7
  • 15
2

You might consider http://code.google.com/p/cpp-stopwatch, it's in plain C++, no dependencies and comes with a handy Visual Studio solution. Oh, and I wrote it.

tunnuz
  • 23,338
  • 31
  • 90
  • 128
1

For what it's worth, here is a Windows implementation (quite similar to .NET):

class Stopwatch
{
    LARGE_INTEGER _startTime;
    LARGE_INTEGER _frequency;

public:
    Stopwatch(bool start = false) :_startTime({ 0 })
    {
        QueryPerformanceFrequency(&_frequency);
        if (start)
        {
            Start();
        }
    }

    LONGLONG GetStartTime() { return _startTime.QuadPart; }
    bool IsStarted() { return _startTime.QuadPart; }
    bool IsStopped() { return !IsStarted(); }

    void Start()
    {
        QueryPerformanceCounter(&_startTime);
    }

    void Stop()
    {
        ZeroMemory(&_startTime, sizeof(LARGE_INTEGER));
    }

    static LONGLONG GetFrequency()
    {
        LARGE_INTEGER li;
        QueryPerformanceFrequency(&li);
        return li.QuadPart;
    }

    static LONGLONG GetTicks()
    {
        LARGE_INTEGER endingTime;
        QueryPerformanceCounter(&endingTime);
        return endingTime.QuadPart;
    }

    LONGLONG GetElapsedTicks(bool restart = false)
    {
        LARGE_INTEGER endingTime;
        QueryPerformanceCounter(&endingTime);
        auto qp = endingTime.QuadPart - _startTime.QuadPart;
        if (restart)
        {
            Start();
        }
        return qp;
    }

    LONGLONG GetElapsed100NanoSeconds(bool restart = false)
    {
        LARGE_INTEGER endingTime;
        QueryPerformanceCounter(&endingTime);

        LARGE_INTEGER elapsed100NanoSeconds{};
        elapsed100NanoSeconds.QuadPart = endingTime.QuadPart - _startTime.QuadPart;
        elapsed100NanoSeconds.QuadPart *= 10000000;
        elapsed100NanoSeconds.QuadPart /= _frequency.QuadPart;
        if (restart)
        {
            Start();
        }
        return elapsed100NanoSeconds.QuadPart;
    }

    LONGLONG GetElapsedMicroseconds(bool restart = false)
    {
        LARGE_INTEGER endingTime;
        QueryPerformanceCounter(&endingTime);

        LARGE_INTEGER elapsedMicroSeconds{};
        elapsedMicroSeconds.QuadPart = endingTime.QuadPart - _startTime.QuadPart;
        elapsedMicroSeconds.QuadPart *= 1000000;
        elapsedMicroSeconds.QuadPart /= _frequency.QuadPart;
        if (restart)
        {
            Start();
        }
        return elapsedMicroSeconds.QuadPart;
    }

    LONGLONG GetElapsedMilliseconds(bool restart = false)
    {
        LARGE_INTEGER endingTime;
        QueryPerformanceCounter(&endingTime);

        LARGE_INTEGER elapsedMilliSeconds{};
        elapsedMilliSeconds.QuadPart = endingTime.QuadPart - _startTime.QuadPart;
        elapsedMilliSeconds.QuadPart *= 1000;
        elapsedMilliSeconds.QuadPart /= _frequency.QuadPart;
        if (restart)
        {
            Start();
        }
        return elapsedMilliSeconds.QuadPart;
    }

    LONGLONG GetElapsedSeconds(bool restart = false)
    {
        LARGE_INTEGER endingTime;
        QueryPerformanceCounter(&endingTime);

        LARGE_INTEGER elapsedSeconds{};
        elapsedSeconds.QuadPart = endingTime.QuadPart - _startTime.QuadPart;
        elapsedSeconds.QuadPart /= _frequency.QuadPart;
        if (restart)
        {
            Start();
        }
        return elapsedSeconds.QuadPart;
    }
};
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
1

You can use QueryPerformanceCounter to get a better timing when "profiling" some code (it's not perfect, but should be enough to get you starter).

BOOL WINAPI QueryPerformanceCounter( __out  LARGE_INTEGER *lpPerformanceCount );

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx.

Max
  • 3,128
  • 1
  • 24
  • 24