2018年3月27日 星期二

[C][time] time function


#include<time.h>
/** Method 1 */ 
time_t timep;

time(&timep);
printf("[Debug] time: %s\n",ctime(&timep));



  static struct timespec start, now;
/** Method 2 */    

#include 
#include 

static struct timespec start, now;

/** @return in ms */
static double time_diff(struct timespec t1, struct timespec t2)
{
    struct timespec diff;
    if (t2.tv_nsec - t1.tv_nsec < 0) {
        diff.tv_sec  = t2.tv_sec - t1.tv_sec - 1;
        diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
    } else {
        diff.tv_sec  = t2.tv_sec - t1.tv_sec;
        diff.tv_nsec = t2.tv_nsec - t1.tv_nsec;
    }
    return (diff.tv_sec * 1000.0 + diff.tv_nsec / 1000000.0);
}


void main(void)
{
    clock_gettime(CLOCK_REALTIME, &start);
    printf("# signal Test\n");
    clock_gettime(CLOCK_REALTIME, &now);
    printf("# signal finish at (%lf) ms\n", time_diff(start, now));
}



0 意見:

張貼留言