C语言跨平台时间操作计算时间差

头文件

#pragma once

#if defined(_WIN32)
#include<sys/timeb.h>
#if defined(__UNIX__)||defined(__APPLE__)
#include<time.h>
#endif

typedef long long long_time_t;

long_time_t TimeInMillisecond(void) {
#if defined(_WIN32)
    struct timeb time_buffer;
    ftime(&time_buffer);
    return time_buffer.time*1000LL+time_buffer.millitm;
#elif defined(__UNIX__)||defined(__APPLE__)
    struct timeval time_value;
    gettimeofday(&time_buffer,NULL);
    return time_buffer.tv_sec*1000LL+time_buffer.tv_usec/1000;
#elif defined(__STDC__)&&__STDC__VERSION__==201112L
    struct timespec timespec_value;
    timespec_get(&timespec_value,TIME_UTC);
    return timespec_value.tv_sec*1000LL+timespec_value.tv_nsec/1000;
#else
    time_t current_time = time(NULL);
    return current_time*1000LL;
#endif
}
#endif

计算时间差

#include<stdio.h>
#include<time.h>
#include"../include/io_utils.h"
#include"../include/time_utils.h"
int main()
{
    clock_t start_time = clock();
    for(int i = 0;i<100;++i)
        printf("hello world!");
    clock_t end_time = clock();
    PRINT_DOUBLE((end_time-start_time)*1.0/CLOCKS_PER_SEC);

    return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃米饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值