c – boost :: thread_sleep vs boost :: chrono :: thread_cloc
发布时间:2020-12-16 06:53:52 所属栏目:百科 来源:网络整理
导读:当我运行此代码时 #include iostream#include boost/thread.hpp#include boost/chrono/thread_clock.hppvoid foo() { boost::this_thread::sleep(boost::posix_time::microseconds(500));}int main() { boost::chrono::thread_clock::time_point start = boo
当我运行此代码时
#include <iostream> #include <boost/thread.hpp> #include <boost/chrono/thread_clock.hpp> void foo() { boost::this_thread::sleep(boost::posix_time::microseconds(500)); } int main() { boost::chrono::thread_clock::time_point start = boost::chrono::thread_clock::now(); foo(); boost::chrono::thread_clock::time_point stop = boost::chrono::thread_clock::now(); std::cout << "duration = " << boost::chrono::duration_cast<boost::chrono::microseconds>(stop-start).count() << " microsecn"; } 我得到以下输出: duration = 121 microsec duration = 121 microsec duration = 110 microsec duration = 114 microsec 此外,当我用不同的值(例如200)替换500时,输出始终大约为100.使用-O3进行编译.为什么时机不一致? PS:我读到睡眠已被弃用,但是当我用它替换它时 boost::this_thread::sleep_for(boost::chrono::microseconds(200)); 输出在~20微秒的范围内. 解决方法
的确,我忽略了一些明显的事情
documentation说:
我认为wall-time是实时传递的,而不是cpu-time,它不包括线程休眠的时间.无论如何,为了获得一致的数字,我不得不使用boost :: chrono :: system_clock. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |