跨平台的计时函数 支持win/linux

博客提及了源文件和头文件,它们是编程中重要的组成部分。源文件包含程序的具体实现代码,头文件则常包含函数声明、宏定义等内容,二者在软件开发中发挥着关键作用。

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

源文件

#if defined(WIN32) || defined(_WIN32)   // Windows system specific
#include <windows.h>
#else          // Unix based system specific
#include <sys/time.h>
#endif

__int64 getCount()
{
#if defined(WIN32) || defined(_WIN32)
    LARGE_INTEGER _count;
    QueryPerformanceCounter(&_count);
    return _count.QuadPart;
#else
    timeval _count;
    gettimeofday(&_count, NULL);
    return (_count.tv_sec * 1000000.0) + _count.tv_usec;
#endif
}

double duration_ms(__int64 start, __int64 end)
{
#if defined(WIN32) || defined(_WIN32)
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
    return (end - start) * (1000.0 / frequency.QuadPart);
#else
    return (end - start) / 1000.0;
#endif
}

double duration_from(__int64 start)
{
#if defined(WIN32) || defined(_WIN32)
    LARGE_INTEGER _count;
    QueryPerformanceCounter(&_count);
    __int64 end = _count.QuadPart;
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
    return (end - start) * (1000.0 / frequency.QuadPart);
#else
    timeval _end;
    gettimeofday(&_end, NULL);
    return ((_end.tv_sec * 1000000.0) + _end.tv_usec - start) / 1000.0;
#endif
}

头文件

#ifndef TIMESTAMP_H
#define TIMESTAMP_H

__int64 getCount();
double duration_ms(__int64 start, __int64 end);
double duration_from(__int64 start);

#endif //TIMESTAMP_H

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值