当我使用 cpulimit 限制程序时,std::chrono 给出异常值
我的代码是这样的:
while(1)
{
std::cout << "local time " << std::chrono<<std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::steady_clock::now()).time_since_epoch()).count() << "\n";
clock_t t1, t2;
t1 = clock();
std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now(), t4;
MyProcessFunc();
t4 = std::chrono::steady_clock::now();
t2 = clock();
std::cout << "process chrono time " << std::chrono::duration_cast<std::chrono::milliseconds>(t4 - t3).count() << "ms\n";
std::cout << "process clock time " << 1000.0*(t2 - t1)/CLOCKS_PER_SEC << "ms\n";
}
当这个程序运行时,我使用“taskset”和“cpulimit”将其限制为仅使用CPU的单个核心以及该单个核心的大约10%。然后我发现 chrono 给出了奇怪的值:
local time 352398168
process chrono time 28ms
process clock time 26.829ms
local time 352398196
process chrono time 808ms
process clock time 26.934ms
local time 352399004
process chrono time 28ms
process clock time 27.168ms
local time 352399032
process chrono time 28ms
process clock time 27ms
local time 352399061
process chrono time 27ms
process clock time 26.931ms
local time 352399089
process chrono time 809ms
process clock time 30.479ms
local time 352399898
process chrono time 33ms
process clock time 32.135ms
我可以感觉到程序运行的卡顿,所以可能 chrono 的结果是正确的。但我认为肯定还有其他地方阻碍了程序的运行。耗时的主要部分不在我的程序上。
任何人都知道为什么plz
My code is like:
while(1)
{
std::cout << "local time " << std::chrono<<std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::steady_clock::now()).time_since_epoch()).count() << "\n";
clock_t t1, t2;
t1 = clock();
std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now(), t4;
MyProcessFunc();
t4 = std::chrono::steady_clock::now();
t2 = clock();
std::cout << "process chrono time " << std::chrono::duration_cast<std::chrono::milliseconds>(t4 - t3).count() << "ms\n";
std::cout << "process clock time " << 1000.0*(t2 - t1)/CLOCKS_PER_SEC << "ms\n";
}
And when this program is running, I use "taskset" and "cpulimit" to restrict it to using only a single core of CPU and about 10% of this single core. Then I found chrono gives werid values:
local time 352398168
process chrono time 28ms
process clock time 26.829ms
local time 352398196
process chrono time 808ms
process clock time 26.934ms
local time 352399004
process chrono time 28ms
process clock time 27.168ms
local time 352399032
process chrono time 28ms
process clock time 27ms
local time 352399061
process chrono time 27ms
process clock time 26.931ms
local time 352399089
process chrono time 809ms
process clock time 30.479ms
local time 352399898
process chrono time 33ms
process clock time 32.135ms
I can feel the stutter of the program running, so may be chrono's result is right. But I think there are definitely other places that block the running of the program. The main part of the time consuming is not on my program.
Any one knows why plz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
clock()
返回程序实际消耗的时间,参见此处,而即使您的程序未运行,std::chrono::steady_clock
也会继续计数。这解释了你的结果。
clock()
returns the time actually consumed by your program, see here, whilestd::chrono::steady_clock
keeps counting even when your program is not running.This explains your results.