在 C 中测量函数的执行时间

新手上路,请多包涵

我想知道某个函数在我的 C++ 程序中在 Linux 上执行需要多少时间。之后,我想做一个速度比较。我看到了几个时间函数,但最终从 boost 中得到了这个。计时:

 process_user_cpu_clock, captures user-CPU time spent by the current process

现在,我不清楚如果我使用上面的函数,我会得到唯一的 CPU 花费在那个函数上的时间吗?

其次,我找不到任何使用上述功能的例子。有人可以帮我如何使用上述功能吗?

PS:现在,我正在使用 std::chrono::system_clock::now() 以秒为单位获取时间,但是由于每次 CPU 负载不同,这给了我不同的结果。

原文由 Xara 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 655
2 个回答

这是 C++11 中非常易于使用的方法。您必须使用来自 std::chrono::high_resolution_clock <chrono> 标头的 —。

像这样使用它:

 #include <chrono>

/* Only needed for the sake of this example. */
#include <iostream>
#include <thread>

void long_operation()
{
    /* Simulating a long, heavy operation. */

    using namespace std::chrono_literals;
    std::this_thread::sleep_for(150ms);
}

int main()
{
    using std::chrono::high_resolution_clock;
    using std::chrono::duration_cast;
    using std::chrono::duration;
    using std::chrono::milliseconds;

    auto t1 = high_resolution_clock::now();
    long_operation();
    auto t2 = high_resolution_clock::now();

    /* Getting number of milliseconds as an integer. */
    auto ms_int = duration_cast<milliseconds>(t2 - t1);

    /* Getting number of milliseconds as a double. */
    duration<double, std::milli> ms_double = t2 - t1;

    std::cout << ms_int.count() << "ms\n";
    std::cout << ms_double.count() << "ms\n";
    return 0;
}

这将测量函数 long_operation 的持续时间。

可能的输出:

 150ms
150.068ms

工作示例: https ://godbolt.org/z/oe5cMd

原文由 Victor 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是一个非常基本的计时器类,您可以根据需要对其进行扩展。我想要一些可以在代码中干净利落地使用的简单的东西。您可以通过以下链接在编码场弄乱它:http: //tpcg.io/nd47hFqr

 class local_timer {
    private:
        std::chrono::_V2::system_clock::time_point start_time;
        std::chrono::_V2::system_clock::time_point stop_time;
        std::chrono::_V2::system_clock::time_point stop_time_temp;
        std::chrono::microseconds most_recent_duration_usec_chrono;
        double most_recent_duration_sec;
    public:

        local_timer() {

        };

        ~local_timer() {

        };

        void start() {
            this->start_time = std::chrono::high_resolution_clock::now();
        };

        void stop() {
            this->stop_time = std::chrono::high_resolution_clock::now();
        };

        double get_time_now() {
            this->stop_time_temp = std::chrono::high_resolution_clock::now();
            this->most_recent_duration_usec_chrono = std::chrono::duration_cast<std::chrono::microseconds>(stop_time_temp-start_time);
            this->most_recent_duration_sec = (long double)most_recent_duration_usec_chrono.count()/1000000;
            return this->most_recent_duration_sec;
        };

        double get_duration() {
            this->most_recent_duration_usec_chrono = std::chrono::duration_cast<std::chrono::microseconds>(stop_time-start_time);
            this->most_recent_duration_sec = (long double)most_recent_duration_usec_chrono.count()/1000000;
            return this->most_recent_duration_sec;
        };

};

这个存在的用途

#include <iostream>
#include "timer.hpp" //if kept in an hpp file in the same folder, can also before your main function

int main() {
    //create two timers
    local_timer timer1 = local_timer();
    local_timer timer2 = local_timer();

    //set start time for timer1
    timer1.start();
    //wait 1 second
    while(timer1.get_time_now() < 1.0) {
    }
    //save time
    timer1.stop();
    //print time
    std::cout << timer1.get_duration() << " seconds, timer 1\n" << std::endl;

    timer2.start();
    for(long int i = 0; i < 100000000; i++) {
        //do something
        if(i%1000000 == 0) {
            //return time since loop started
            std::cout << timer2.get_time_now() << " seconds, timer 2\n"<< std::endl;
        }

    }
    return 0;
}

原文由 its_groovy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题