Visual C++ Stopwatch
Asked Answered
S

4

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.

Spectrophotometer answered 6/2, 2012 at 14:0 Comment(3)
Perhaps you could use the standard clock() function?Adriatic
the reverse question with an example of c++ here : #7726886Signpost
thx ! Actually your question is the answer i wanted !!!Spectrophotometer
R
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

Ricoricochet answered 6/2, 2012 at 14:14 Comment(0)
C
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.

Calamity answered 20/6, 2012 at 9:28 Comment(0)
J
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.

Jonna answered 6/2, 2012 at 15:49 Comment(0)
B
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;
    }
};
Buitenzorg answered 10/6, 2021 at 16:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.