gpt4 book ai didi

c - 将 time_t 设置为毫秒

转载 作者:行者123 更新时间:2023-12-02 20:13:47 42 4
gpt4 key购买 nike

我有一个函数,我希望该函数在运行一定毫秒数后停止运行。这个函数可以工作几秒钟,但我想测试它几毫秒。我该怎么做呢?如果我设置消除= 1,它对应于1秒。如何设置消除 = 5 毫秒?

功能:

void clsfy_proc(S_SNR_TARGET_SET pSonarTargetSet, unsigned char *target_num, time_t eliminate)
{

// get timing
time_t _start = time(NULL);
time_t _end = _start + eliminate;
int _eliminate = 0;

//some code

time_t start = time(NULL);
time_t end = start + eliminate;

for(_tidx = 0; _tidx < pSonarTargetSet[_i].num; _tidx++) {
// check timing
time_t _current = time(NULL);
if (_current > _end) {
printf("clsfy_proc(1), Eliminate due to timeout\n");
_eliminate = 1;
break;
}

//some code

if (_eliminate == 1)
break;
}
//some code
}

最佳答案

time_t 是绝对时间,表示为自 UNIX 纪元(格林尼治标准时间午夜,1970 年 1 月 1 日)以来的整数秒数。它作为时间点的明确且易于使用的表示非常有用。

clock_t 是时间的相对测量,由某个时间点以来的整数个时钟滴答表示(可能是计算机的启动,但不能保证,因为它可能经常翻转)。每秒有 CLOCKS_PER_SEC 时钟滴答数;该常量的值在不同的操作系统之间可能有所不同。它有时用于计时目的,但由于其分辨率相对较低,因此不太擅长。

clock_t的一个小例子:

#include <time.h>
#include <stdio.h>

int main () {
clock_t start_t, end_t, total_t;
int i;

start_t = clock();
printf("Starting of the program, start_t = %ld\n", start_t);

for(i=0; i< 10000000; i++) { }

end_t = clock();
printf("End of the big loop, end_t = %ld\n", end_t);

total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %f\n", total_t );

return(0);
}

关于c - 将 time_t 设置为毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52869470/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com