执行速度测试
大家好,让我说我有这样的东西:
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论