用C+度量函数的执行时间我想知道某个函数在我的C+程序中执行所需的时间。linux..之后,我想做一个速度比较。我看到了几个时间函数,但最后得到的是Boost。编年史:process_user_cpu_clock, captures user-CPU time spent by the current process现在,我不清楚我是否使用了上述功能,我会得到CPU在这个功能上花费的唯一时间吗?其次,我找不到任何使用上述功能的例子。有谁能帮我使用上面的功能吗?P.S:现在,我在用std::chrono::system_clock::now()获得时间的秒,但这给我不同的结果,因为不同的CPU负载,每次。
3 回答
青春有我
TA贡献1784条经验 获得超8个赞
std::chrono::high_resolution_clock<chrono>
#include <iostream>#include <chrono>using namespace std;using namespace std::chrono;void function(){
long long number = 0;
for( long long i = 0; i != 2000000; ++i )
{
number += 5;
}}int main(){
high_resolution_clock::time_point t1 = high_resolution_clock::now();
function();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
cout << duration;
return 0;}注:
大话西游666
TA贡献1817条经验 获得超14个赞
#include <iostream>#include <ctime> // time_t#include <cstdio>void function(){
for(long int i=0;i<1000000000;i++)
{
// do nothing
}}int main(){time_t begin,end; // time_t is a datatype to store time values.time (&begin);
// note time before executionfunction();time (&end);
// note time after executiondouble difference = difftime (end,begin);
printf ("time taken for function() %.2lf seconds.\n", difference );return 0;}- 3 回答
- 0 关注
- 405 浏览
添加回答
举报
0/150
提交
取消
