编译后,如何使其显示计算时间?

c++ visual studio10 After compiling, how do i make it so that it displays the calculation time

本文关键字:计算 时间 显示 何使其 编译      更新时间:2023-10-16

我不知道你怎么称呼它…使用visualstudio10编译后,会弹出黑屏显示结果。

我注意到在youtube视频上,在他们的屏幕上,它也显示了计算所需的时间。(未编译时间)

我很确定答案就在那里,但我不知道搜索的关键字

这是一个我用来计时的小秒表类。准确性是……嗯…你做得越多越好,就这么说吧。

    #include <sstream>
    #include <chrono>
    class Stopwatch final
    {
    public:
        Stopwatch()
        {
            Reset();
        }
        void Reset()
        {
            MyCurrentTime = MyClock.now();
        }
        double Elapsed() const
        {
            auto elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(MyClock.now() - MyCurrentTime);
            return elapsed.count();
        }
        int ms() const
        {
            return static_cast<int>(0.5 + Elapsed() * 1000);
        }
        std::string ToMilliseconds() const
        {
            auto o = std::ostringstream();
            o << ms();
            return o.str();
        }
        std::string ToSeconds(int precision) const
        {
            auto o = std::ostringstream();
            o.precision(precision);
            o << std::fixed << Elapsed();
            return o.str();
        }
    private:
        std::chrono::high_resolution_clock MyClock;
        std::chrono::high_resolution_clock::time_point MyCurrentTime;
    };

一个小例子:

omp_get_wtime

#include <omp.h>
int main(void)
{
    double dStart = omp_get_wtime();
    Calculations();
    double dEnd = omp_get_wtime();
    std::cout << "Calculation took: " << dEnd - dStart << " sec." std::endl;
    return 0;
}