[英]Confusion about Timing Measurements
I have been tasked with implementing three different functions get_current_time_seconds1, 2 and 3, and then have to estimate the resolution of the various functions. 我的任务是实现三个不同的函数get_current_time_seconds1、2和3,然后必须估算各种函数的分辨率。 How would I estimate this? 我怎么估计呢?
Which timing function would you suggest to use? 您建议使用哪个计时功能? What do the compiler options -O0 -lrt mean when I have to compile with gcc -O0 -lrt timing.c -o timing? 当我必须使用gcc -O0 -lrt Timing.c -o Timing进行编译时,编译器选项-O0 -lrt是什么意思?
#define BILLION 1000000000L
#define LIMIT_I 1000
#define LIMIT_J 1000
double get_current_time_seconds1()
{
/* Get current time using gettimeofday */
time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("%s\n", asctime(tm));
return (double) tm;
}
double get_current_time_seconds2()
{
struct timespec start,stop;
clock_gettime(CLOCK_REALTIME, &start);
clock_gettime(CLOCK_REALTIME, &stop);
double x = (stop.tv_sec - start.tv_sec) + (stop.tv_nsec - start.tv_nsec);
printf("%lf\n", x);
return (double) x;
}
double get_current_time_seconds3()
{
uint64_t diff;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
sleep(5);
clock_gettime(CLOCK_MONOTONIC, &end);
diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time = %llu nanoseconds\n", (long long unsigned int)diff);
return (double) diff;
}
How would I estimate this? 我怎么估计呢? Which timing function would you suggest to use? 您建议使用哪个计时功能?
If you want to get the resolution (precision) of the various timing elements, you can use the clock_getres
function, passing in the various CLOCK_
id types, for example: 如果要获取各种计时元素的分辨率(精度),则可以使用clock_getres
函数,传入各种CLOCK_
id类型,例如:
#include <stdio.h>
#include <time.h>
static void printres(clockid_t id)
{
struct timespec ts;
int rc = clock_getres(id, &ts);
printf("clock id: %d\n", (unsigned int)id);
if (rc != 0) {
printf("Error: %d\n", rc);
return;
}
printf("tv_sec = %lu\ntv_nsec = %lu\n", ts.tv_sec, ts.tv_nsec);
}
int main(int argc, char** argv)
{
printres(CLOCK_REALTIME);
printres(CLOCK_MONOTONIC);
printres(CLOCK_PROCESS_CPUTIME_ID);
printres(CLOCK_THREAD_CPUTIME_ID);
return 0;
}
On my system, tv_nsec
for all but CLOCK_THREAD_CPUTIME_ID
is 1000
, for CLOCK_THREAD_CPUTIME_ID
the value for tv_nsec
is 1
, this means that the precision for the other clock types is 1 millisecond (1000 nanoseconds) while the precision of CLOCK_THREAD_CPUTIME_ID
is 1 nanosecond. 在我的系统, tv_nsec
为所有,但CLOCK_THREAD_CPUTIME_ID
是1000
,对于CLOCK_THREAD_CPUTIME_ID
的价值tv_nsec
为1
,这意味着对于其他类型的时钟精度为1毫秒(1000纳秒),而精度CLOCK_THREAD_CPUTIME_ID
是1纳秒。
For your first function that calls localtime
the precision for that would be 1 second as that function calculates the time from the Unix epoch in seconds. 对于第一个调用localtime
的函数,其精度为1秒,因为该函数以秒为单位计算Unix时期的时间。
What do the compiler options -O0 -lrt mean when I have to compile with gcc -O0 -lrt timing.c -o timing? 当我必须使用gcc -O0 -lrt Timing.c -o Timing进行编译时,编译器选项-O0 -lrt是什么意思?
For some compilers like gcc
and clang
the option -O
means to optimize the code when compiling it to the level specified, so -O0
means not to optimize the code at all, this is usually useful when debugging code. 对于gcc
和clang
等某些编译器,选项-O
表示在将代码编译到指定级别时优化代码,因此-O0
表示根本不优化代码,这在调试代码时通常很有用。
The -l
option says to compile against the specified library, so -lrt
says to compile using the rt
library, or "real time library"; -l
选项表示要针对指定的库进行编译,因此-lrt
表示要使用rt
库或“实时库”进行编译; this is necessary on some systems as CLOCK_REALTIME
can be defined in that library. 在某些系统上这是必需的,因为可以在该库中定义CLOCK_REALTIME
。
I hope that can help. 希望能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.