当我使用 cpulimit 限制程序时,std::chrono 给出异常值

发布于 2025-01-09 05:34:07 字数 1464 浏览 0 评论 0原文

我的代码是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2025-01-16 05:34:07

clock() 返回程序实际消耗的时间,参见此处,而即使您的程序未运行,std::chrono::steady_clock 也会继续计数。

这解释了你的结果。

clock() returns the time actually consumed by your program, see here, while std::chrono::steady_clock keeps counting even when your program is not running.

This explains your results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文