一、struct timespec 定义:
typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数 int clock_gettime(clockid_t clk_id, struct timespec *tp);
其中,cld_id类型四种:
a、CLOCK_REALTIME: 系统实时时间,随系统实时时间改变而改变
b、CLOCK_MONOTONIC, 从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
c、CLOCK_PROCESS_CPUTIME_ID, 本进程到当前代码系统CPU花费的时间
d、CLOCK_THREAD_CPUTIME_ID, 本线程到当前代码系统CPU花费的时间
本文默认采用CLOCK_REALTIME,即可实现并行程序的准确计时。
struct tm *localtime(const time_t *clock); //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
二、struct timeval 定义:
struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};
struct timezone{
int tz_minuteswest; //miniutes west of Greenwich
int tz_dsttime; //type of DST correction
};
struct timeval有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。
一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间
#include<stdio.h>
#include<time.h>
#include<sys/time.h>
void nowtime_ns()
{
printf("---------------------------struct timespec---------------------------------------\n");
printf("[time(NULL)] : %ld\n", time(NULL));
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec);
struct tm t;
char date_time[64];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));
printf("clock_gettime : date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
}
void nowtime_us()
{
printf("---------------------------struct timeval----------------------------------------\n");
printf("[time(NULL)] : %ld\n", time(NULL));
struct timeval us;
gettimeofday(&us,NULL);
printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);
struct tm t;
char date_time[64];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
}
int main(int argc, char* argv[])
{
nowtime_ns();
printf("\n");
nowtime_us();
printf("\n");
return 0;
}
1 #include <time.h>
2
3 // 返回自系统开机以来的毫秒数(tick)
4 unsigned long GetTickCount()
5 {
6 struct timespec ts;
7
8 clock_gettime(CLOCK_MONOTONIC, &ts);
9
10 return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
11 }
12
13
14 int main()
15 {
16 struct timespec time1 = { 0, 0 };
17
18 clock_gettime(CLOCK_REALTIME, &time1);
19 printf("CLOCK_REALTIME: %d, %d\n", time1.tv_sec, time1.tv_nsec);
20
21 clock_gettime(CLOCK_MONOTONIC, &time1);
22 printf("CLOCK_MONOTONIC: %d, %d\n", time1.tv_sec, time1.tv_nsec);
23
24 clock_gettime(CLOCK_MONOTONIC_RAW, &time1);
25 printf("CLOCK_MONOTONIC_RAW: %d, %d\n", time1.tv_sec, time1.tv_nsec);
26
27 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
28 printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d\n", time1.tv_sec,
29 time1.tv_nsec);
30
31 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
32 printf("CLOCK_THREAD_CPUTIME_ID: %d, %d\n", time1.tv_sec,
33 time1.tv_nsec);
34
35 printf("\n%d\n", time(NULL));
36
37 printf("tick count in ms: %ul\n", GetTickCount());
38
39 return 0;
40 }