测试方法
gettimeofday 和 clock_gettime的7种类型时间的对比。
clock_gettime类型如下
类型 | 含义 |
---|---|
CLOCK_REALTIME | 从1970.1.1到目前的时间 |
CLOCK_MONOTONIC | 系统启动到现在的时间 |
CLOCK_THREAD_CPUTIME_ID | 线程所消耗的时间 |
CLOCK_PROCESS_CPUTIME_ID | 进程所消耗的时间 |
CLOCK_MONOTONIC_RAW | 基于硬件时间 |
CLOCK_REALTIME_COARSE | 不精确的REALTIME |
CLOCK_MONOTONIC_COARSE | 不精确的启动时间 |
固定执行次数,统计消耗时间。
测试代码
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
//#define LOOP_TIMES 10000000
#define LOOP_TIMES 5000000
#define GTOD_FLAG ~0
const char* timetype_str[7] = {
"CLOCK_REALTIME",
"CLOCK_MONOTONIC",
"CLOCK_THREAD_CPUTIME_ID",
"CLOCK_PROCESS_CPUTIME_ID",
"CLOCK_MONOTONIC_RAW",
"CLOCK_REALTIME_COARSE",
"CLOCK_MONOTONIC_COARSE"
};
// flag GTOD_FLAG getimeofday
static inline void do_gettime(unsigned int flag, int loop_times)
{
int i = 0;
struct timeval tv;
struct timespec time1 = {0, 0};
if (flag == GTOD_FLAG) {
for (i = 0; i < loop_times; i++) {
gettimeofday (&tv, NULL);
}
} else {
for (i = 0; i < loop_times; i++) {
clock_gettime(flag, &time1);
}
}
}
// flag ~0 gettimeofday, flag 0 - 3, clock_gettime
int gettime_test(unsigned int flag, int loop_times)
{
int i = 0;
long s_intval;
long us_intval;
long interval = 0;
struct timeval begin;
struct timeval end;
gettimeofday (&begin, NULL);
do_gettime(flag, loop_times);
gettimeofday (&end, NULL);
s_intval = end.tv_sec - begin.tv_sec;
us_intval = end.tv_usec - begin.tv_usec;
interval = s_intval * 1000 + us_intval/1000;
printf("%d times cost %ldms\n", loop_times, interval);
return 0;
}
void gettimeofday_test(int loop_times)
{
printf("gettimeofday\n");
gettime_test(GTOD_FLAG, loop_times);
printf("\n");
}
void clock_gettime_test(int loop_times, unsigned int flag)
{
printf("clock_gettime type: %s\n", timetype_str[flag]);
gettime_test(flag, loop_times);
printf("\n");
}
int main()
{
int flag = 0;
gettimeofday_test(LOOP_TIMES);
for (flag = 0; flag < 7; flag++)
clock_gettime_test(LOOP_TIMES, flag);
return 0;
}
测试结果
显而易见,gettimeofday()性能明显优势,clock_gettime各种类型时间获取差距不大,不精确的时间稍微快一点。
吐槽一下笔记本CPU单核性能太弱,8750H跑这个循环500万次,比家里台式机2700x跑这个循环1000万次的时间还长。
ckun@ubuntu:~/gettime$ ./a.out
gettimeofday
5000000 times cost 3325ms
clock_gettime type: CLOCK_REALTIME
5000000 times cost 4067ms
clock_gettime type: CLOCK_MONOTONIC
5000000 times cost 4140ms
clock_gettime type: CLOCK_THREAD_CPUTIME_ID
5000000 times cost 4408ms
clock_gettime type: CLOCK_PROCESS_CPUTIME_ID
5000000 times cost 4167ms
clock_gettime type: CLOCK_MONOTONIC_RAW
5000000 times cost 4091ms
clock_gettime type: CLOCK_REALTIME_COARSE
5000000 times cost 3914ms
clock_gettime type: CLOCK_MONOTONIC_COARSE
5000000 times cost 4068ms
ckun@ubuntu:~/gettime$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 36 bits physical, 48 bits virtual
CPU(s): 12
On-line CPU(s) list: 0-11
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Stepping: 10
CPU MHz: 2201.000
CPU max MHz: 2201.0000
BogoMIPS: 4402.00