星期五, 四月 04, 2014

C如何得到当前时间时间戳用微秒

最简单代码。

#include <sys/time.h>
#include <inttypes.h>
uint64_t getCurrentTimestamp()
{
struct timeval tv;
gettimeofday(&tv,NULL);

printf("%d raw microseconds:\n",tv.tv_usec);
 uint64_t time_in_micros =( (uint64_t)1000 * (tv.tv_sec) )+ tv.tv_usec/1000;
printf("%" PRIu64 "  real microseconds:\n",time_in_micros);
return time_in_micros;
}