gettimeofday和clock_gettime性能对比

本文通过实验对比了gettimeofday和clock_gettime不同类型的性能差异。结果显示gettimeofday表现更优,而clock_gettime的不同类型间差异不大,不精确的时间类型稍快。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试方法

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值