在Clion中查找运行时间
在Clion中查找运行时间
我正在尝试使用time()函数来测量我的程序的各个点。\n我不明白的是为什么before和after的值是一样的?我知道这不是最好的性能分析方法,我只是想看看某个操作花费了多长时间。\n我尝试了以下代码:\n
printf("**MyProgram::before time= %ld\n", time(NULL)); doSomething(); doSomethingLong(); printf("**MyProgram::after time= %ld\n", time(NULL));
\n我还尝试了以下代码:\n
struct timeval diff, startTV, endTV; gettimeofday(&startTV, NULL); doSomething(); doSomethingLong(); gettimeofday(&endTV, NULL); timersub(&endTV, &startTV, &diff); printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
\n我如何解读**time taken = 0 26339
的结果?这是表示26,339纳秒 = 26.3毫秒吗?\n那么**time taken = 4 45025
表示4秒和25毫秒吗?
问题的出现原因:使用clock函数来测量运行时间时,并不准确地反映实际时间的流逝,因为它只能测量CPU时间而不是实际时间。
解决方法:可以尝试使用其他方法来测量实际时间的流逝。某些情况下了使用C++11的chrono库来测量墙上的时间,还有其他一些解决方案被提到,但并不是所有的解决方案都被认为是有效的或可靠的。
文章内容如下:
在使用Clion进行编程时,我们常常需要测量代码的运行时间。在C++中,有几种方法可以实现这一点,其中一种方法是使用clock函数。然而,我们需要注意的是,clock函数只能测量CPU时间,而不是实际时间的流逝。
在下面的代码示例中,我们可以看到如何使用clock函数来测量代码的运行时间:
#includevoid f() { using namespace std; clock_t begin = clock(); code_to_time(); clock_t end = clock(); double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; }
尽管这种方法是简单且易于移植的,但它并不能准确地反映实际时间的流逝。事实上,clock函数只精确到秒,而每秒钟有CLOCKS_PER_SEC个时钟滴答。因此,它并不是一个准确的测量方法。
特别是在编写并行代码时,使用clock函数来测量运行时间并不能反映真实世界中的时间流逝。为了解决这个问题,有人提出了使用C++11的chrono库来测量墙上的时间。这种方法可以更准确地测量实际时间的流逝,而不受CPU时间的限制。以下是一个示例代码:
#includevoid f() { using namespace std::chrono; auto start = high_resolution_clock::now(); code_to_time(); auto end = high_resolution_clock::now(); auto duration = duration_cast (end - start); double elapsed_secs = duration.count(); }
除了使用C++11的chrono库之外,还有其他一些解决方案被提到。然而,并不是所有的解决方案都被认为是有效的或可靠的。某些情况下了使用clock函数的POSIX兼容编译器的解决方案,但并不适用于所有编译器。此外,还有其他一些解决方案被提到,但并没有得到广泛的认可或验证。
在使用Clion进行编程时,我们需要谨慎选择测量代码运行时间的方法。尽管使用clock函数是一种简单且易于移植的方法,但它并不准确地反映实际时间的流逝。因此,我们可以尝试使用C++11的chrono库或其他解决方案来测量墙上的时间,以获得更准确和可靠的结果。
在Clion中查找运行时间的原因是为了测量代码的执行时间,以便进行性能分析和优化。以下是一些解决方法:
方法0:使用delta函数计算时间差异
这种方法使用了一个delta函数来计算时间差异。它接受任何时间点并产生任何持续时间,默认为毫秒。具体实现如下:
auto start = std::chrono::steady_clock::now(); std::cout << "Elapsed(ms)=" << since(start).count() << std::endl; template < class result_t = std::chrono::milliseconds, class clock_t = std::chrono::steady_clock, class duration_t = std::chrono::milliseconds > auto since(std::chrono::time_pointconst& start) { return std::chrono::duration_cast (clock_t::now() - start); }
方法1:使用基于std::chrono的计时器
这种方法使用了一个基于std::chrono的计时器来测量代码的执行时间。具体实现如下:
Timer clock; // Timerclock.tick(); /* code you want to measure */ clock.tock(); cout << "Run time = " << clock.duration().count() << " ms\n"; template class Timer { using timep_t = typename ClockT::time_point; timep_t _start = ClockT::now(), _end = {}; public: void tick() { _end = timep_t{}; _start = ClockT::now(); } void tock() { _end = ClockT::now(); } template auto duration() const { gsl_Expects(_end != timep_t{} && "toc before reporting"); return std::chrono::duration_cast (_end - _start); } }
方法2:对函数进行仪器化
这种方法可以对可调用对象进行仪器化,以测量其执行时间。具体实现如下:
cout << "F runtime=" << measure<>::duration(F, arg1, arg2).count() << "ms"; templatestruct measure { template static auto duration(F&& func, Args&&... args) { auto start = ClockT::now(); std::invoke(std::forward (func), std::forward (args)...); return std::chrono::duration_cast (ClockT::now()-start); } }
通过使用这些方法,可以在Clion中测量代码的运行时间,并进行性能分析和优化。
在Clion中测量运行时间的问题的原因是没有正确地包含#include <chrono>
指令。解决方法是添加该指令,并将报告时间的代码更改为std::cout << "Time difference (sec) = " << (std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()) /1000000.0 <<std::endl;
。同时,在编译时不要忘记添加C++11标志-std=c++11
。
根据cppreference上的文档,“这个时钟与墙上时钟时间无关(例如,它可以是自上次重启以来的时间),最适合用于测量间隔时间。”如果想要以秒为单位返回持续时间,应该仔细查看std::chrono::duration_cast,特别是std::chrono::seconds。
在std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()中,std::chrono::microseconds::rep是数据类型。可以从chrono duration文档中找到这些类型。这个答案使用了完整的类型而不是"auto",这样更容易理解。据说它是std::chrono::microseconds::rep类型,即长整型long int。尽管有人错误地打了C++标签,但这显然是一个C问题。