小编shi*_*ino的帖子

gettimeofday 函数的精度是多少?

我正在阅读 OSTEP 的single.dvi章节。在作业部分,它说:

\n
\n

您\xe2\x80\x99必须考虑的一件事是计时器的精度和准确度。您可以使用的典型计时器是gettimeofday();\n阅读手册页以获取详细信息。您\xe2\x80\x99 将看到gettimeofday()\n返回自 1970 年以来以微秒为单位的时间;然而,这并不意味着计时器精确到微秒。测量连续调用\ngettimeofday()以了解计时器的精确度\n;这将告诉您必须运行多少次空系统调用\n测试\xe2\x80\x99 迭代才能获得良好的测量结果。如果\ngettimeofday()对您来说不够精确,您可以考虑\nrdtsc使用 x86 计算机上可用的指令

\n
\n

我编写了一些代码来测试调用函数的成本,gettimeofday()如下所示:

\n
#include <stdio.h>\n#include <sys/time.h>\n\n#define MAX_TIMES 100000\n\nvoid m_gettimeofday() {\n    struct timeval current_time[MAX_TIMES];\n    int i;\n    for (i = 0; i < MAX_TIMES; ++i) {\n        gettimeofday(&current_time[i], NULL);\n    }\n    printf("seconds: %ld\\nmicro_seconds: %ld\\n", current_time[0].tv_sec, current_time[0].tv_usec);\n    printf("seconds: %ld\\nmicro_seconds: %ld\\n", current_time[MAX_TIMES - 1].tv_sec, current_time[MAX_TIMES - 1].tv_usec);\n    printf("the average time of a gettimeofday function call is: %ld us\\n", (current_time[MAX_TIMES - …
Run Code Online (Sandbox Code Playgroud)

c gettimeofday

5
推荐指数
1
解决办法
1628
查看次数

关于构建编译器扫描器的问题

我想从头开始构建一个编译器。所以第一步,我想构建一个扫描仪。但我遇到了一个问题,我应该如何对待关系运算符,例如“>=”、“==”、“<=”?我应该简单地将它们视为 "> then =、= then =、< then =" 还是作为一个整体?顺便问一下,“++”和“--”怎么样?感谢帮助!

compiler-construction

0
推荐指数
1
解决办法
270
查看次数

标签 统计

c ×1

compiler-construction ×1

gettimeofday ×1