Linux下时间函数:struct timeval结构体

 Date: 2017/4/28


Linux时间函数的结构体如下:

#include "sys/time.h"

struct timeval  
{  
__time_t tv_sec;        /* Seconds. */  
__suseconds_t tv_usec;  /* Microseconds. */  
}; 

用法很简单,如下例所示:

(1)定义时间结构体变量:

struct timeval tv_b,tv_d;

unsigned long long timeconsumed = 0;

(2)获取当前时间:

gettimeofday(&tv_b,NULL);

/*  a piece of codes ......*/
gettimeofday(&tv_d,NULL);

(3)时间统计:

timeconsumed = tv_d.tv_sec-tv_b.tv_sec +(tv_d.tv_usev-tv_b.tv_usec)/1000000;//以秒为单位

THE END!

 

 

 

你可能感兴趣的:(Linux下时间函数:struct timeval结构体)