将返回 CPU 时间提升为 0

Boost returning CPU time of 0

本文关键字:时间 返回 CPU      更新时间:2023-10-16

我正在尝试测量C++程序中函数的CPU时间。我正在使用提升库来执行此操作。但是,当我运行程序时,我得到的结果为 0 秒。我是新手,所以如果有人能指出我正确的方向?

如何使此返回成为我可以实际使用的时间,例如 0.00156 或其他什么?

代码:在 main(( 内部

boost::timer::cpu_timer timer;
std::cout << tree1.search("Cork") << std::endl;
//Print CPU TIME
boost::timer::cpu_times elapsed = timer.elapsed();
std::cout << " CPU TIME: " << (elapsed.user + elapsed.system) / 1e9 << " seconds" << std::endl;

您可以使用boost chrono ;

boost::timer::cpu_timer timer;
std::cout << tree1.search("Cork") << std::endl;
//Print CPU TIME
boost::chrono::duration<double> elapsed = boost::chrono::nanoseconds(timer.elapsed().user);
std::cout << " CPU TIME: " << seconds.count() << "sn" << " seconds" << std::endl;