繁体   English   中英

将time_t设置为毫秒

[英]Set time_t to milliseconds

我有一个函数,我希望函数一旦运行了一定的毫秒数就停止运行。 此功能可以工作几秒钟,但我想测试一下毫秒。 我该怎么做呢? 如果我设置消除= 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);
}

您可以使用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); 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM