2

我有一个函数,我希望该函数在运行一定毫秒后停止运行。这个函数可以工作几秒钟,但我想测试几毫秒。我该怎么做呢?如果我设置消除 = 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 
}
4

2 回答 2

5

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);
}
于 2018-10-18T08:12:12.057 回答
0

您可以使用 getrusage()。请看示例:

资料来源: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); 
} 
于 2018-10-18T13:05:50.257 回答