Visual C++跑表

发布于 2025-01-03 02:11:46 字数 104 浏览 1 评论 0原文

我想知道测量 C++ 中某些代码的执行时间的最佳方法是什么。是否有内置的秒表(.Net)类似类?我正在VS2010上开发C++。我如何(如果)在我的 C++ 项目中使用 .Net 库?先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人间☆小暴躁 2025-01-10 02:11:46

AFAIK C++ 还没有像 .NET 中的 Stopwatch 这样的标准类。

http://cplus.about.com/od/howtodothingsi2/a/timing.htm 是windows平台上高分辨率定时器的示例。

此类计时器的独立于平台的实现是: http://www.boost.org/ libs/timer/doc/index.html

HTH

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

何以笙箫默 2025-01-10 02:11:46

您可以考虑http://code.google.com/p/cpp-stopwatch,它采用纯 C++ 编写,没有依赖项,并附带方便的 Visual Studio 解决方案。哦,我写的。

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.

不必了 2025-01-10 02:11:46

您可以使用 QueryPerformanceCounter 在“分析”某些代码时获得更好的时机(它并不完美,但应该足以让您入门)。

BOOL WINAPI QueryPerformanceCounter( __out  LARGE_INTEGER *lpPerformanceCount );

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

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.

冷︶言冷语的世界 2025-01-10 02:11:46

就其价值而言,这里是一个 Windows 实现(与 .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;
    }
};

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;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文