[英]Set time_t to milliseconds
I have a function and I want the function to stop running once it has been running for a certain number of milliseconds. 我有一个函数,我希望函数一旦运行了一定的毫秒数就停止运行。 This function works for seconds but I want to test it for milliseconds. 此功能可以工作几秒钟,但我想测试一下毫秒。 How do I do this? 我该怎么做呢? If I set eliminate = 1, it corresponds to 1 second. 如果我设置消除= 1,则相当于1秒。 How do I set eliminate = 5 ms? 如何设置消除= 5毫秒?
Function: 功能:
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
is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). time_t
是一个绝对时间,表示为UNIX纪元(格林尼治标准时间1970年1月1日午夜)以来的整数秒数。 It is useful as an unambiguous, easy-to-work-with representation of a point in time. 它可以用作时间点的清晰,易于操作的表示形式。
clock_t
is a relative measurement of time, represented by an integer number of clock ticks since some point in time (possibly the computer's bootup, but no guarantees, as it may roll over quite often). clock_t
是时间的相对量度,由某个时间点以来的整数时钟滴答表示(可能是计算机的启动,但不能保证,因为它可能会经常滚动)。 There are CLOCKS_PER_SEC clock ticks per second; 每秒有CLOCKS_PER_SEC个时钟滴答; the value of this constant can vary between different operating systems. 该常数的值在不同的操作系统之间会有所不同。 It is sometimes used for timing purposes, but it is not very good at it due to its relatively low resolution. 它有时用于计时目的,但由于其分辨率相对较低,因此效果不佳。
One small example for clock_t
: 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);
}
You can use getrusage(). 您可以使用getrusage()。 Please see the example: 请参见示例:
Source: http://www.cs.tufts.edu/comp/111/examples/Time/getrusage.c 来源: http : //www.cs.tufts.edu/comp/111/examples/Time/getrusage.c
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
///////////////////////////////////
// measure user and system time using the "getrusage" call.
///////////////////////////////////
//struct rusage {
// struct timeval ru_utime; /* user CPU time used */
// struct timeval ru_stime; /* system CPU time used */
// long ru_maxrss; /* maximum resident set size */
// long ru_ixrss; /* integral shared memory size */
// long ru_idrss; /* integral unshared data size */
// long ru_isrss; /* integral unshared stack size */
// long ru_minflt; /* page reclaims (soft page faults) */
// long ru_majflt; /* page faults (hard page faults) */
// long ru_nswap; /* swaps */
// long ru_inblock; /* block input operations */
// long ru_oublock; /* block output operations */
// long ru_msgsnd; /* IPC messages sent */
// long ru_msgrcv; /* IPC messages received */
// long ru_nsignals; /* signals received */
// long ru_nvcsw; /* voluntary context switches */
// long ru_nivcsw; /* involuntary context switches */
//};
//struct timeval
// {
// long int tv_sec; /* Seconds. */
// long int tv_usec; /* Microseconds. */
// };
main () {
struct rusage buf;
// chew up some CPU time
int i,j; for (i=0,j=0; i<100000000; i++) { j+=i*i; }
getrusage(RUSAGE_SELF, &buf);
printf("user seconds without microseconds: %ld\n", buf.ru_utime.tv_sec);
printf("user microseconds: %ld\n", buf.ru_utime.tv_usec);
printf("total user seconds: %e\n",
(double) buf.ru_utime.tv_sec
+ (double) buf.ru_utime.tv_usec / (double) 1000000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.