执行速度测试

发布于 2025-02-13 19:13:05 字数 795 浏览 0 评论 0原文

大家好,让我说我有这样的东西:

#include <time.h>
#include <unistd.h>
#include <iostream>
#include <sys/time.h>

void
test()
{
    /* Some tests */
    for (int i = 0; i < 1000; ++i)
    {
    }
}

void
testing(void(*tester)())
{
    for (int i = 0; i < 100; ++i)
    {
        struct timeval start;
        struct timeval stop;
        gettimeofday(&start, NULL);
        (*tester)();
        gettimeofday(&stop, NULL);
        usleep(2000);
        std::cout << (float)((stop.tv_sec * (int)1e6 + stop.tv_usec) - (start.tv_sec * (int)1e6 + start.tv_usec)) << std::endl;
    }
}

int
main(int argc, char const *argv[])
{
    testing(&test);
    return 0;
}

在这里我正在衡量功能的执行时间,有人可以向我解释为什么Usleep可以影响我在这里测量的时间?既然它在关键的测量区域之外?

Hello everyone lets say i have something like this :

#include <time.h>
#include <unistd.h>
#include <iostream>
#include <sys/time.h>

void
test()
{
    /* Some tests */
    for (int i = 0; i < 1000; ++i)
    {
    }
}

void
testing(void(*tester)())
{
    for (int i = 0; i < 100; ++i)
    {
        struct timeval start;
        struct timeval stop;
        gettimeofday(&start, NULL);
        (*tester)();
        gettimeofday(&stop, NULL);
        usleep(2000);
        std::cout << (float)((stop.tv_sec * (int)1e6 + stop.tv_usec) - (start.tv_sec * (int)1e6 + start.tv_usec)) << std::endl;
    }
}

int
main(int argc, char const *argv[])
{
    testing(&test);
    return 0;
}

Here I am measuring the execution time of a function, can someone explain to me why usleep can have an impact on the time I am measuring here? Since it is outside the critical measurement zone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文