c++时间函数性能对比

uint64_t getCurrentTime1()
{
    struct timeb rawtime;
    ftime(&rawtime);
    return rawtime.time * 1000 + rawtime.millitm;
}

uint64_t getCurrentTime2()
{
    return GetTickCount64();
}

uint64_t  getCurrentTime3()
{
    uint64_t current_tics;
    FILETIME ct;

    GetSystemTimeAsFileTime(&ct);

    current_tics = (unsigned __int64)ct.dwLowDateTime +(((unsigned __int64)ct.dwHighDateTime) << 32);
    return current_tics;
}
                
unsigned int day[] = {0,31,59,90,120,151,181,212,243,273,304,334 }; 

uint64_t getCurrentTime4()
{

    LARGE_INTEGER count, freq;
    BOOL br = QueryPerformanceCounter(&count);
    br = QueryPerformanceFrequency(&freq);
    return count.QuadPart * 1000000000 / freq.QuadPart;


}

uint64_t getCurrentTime5()
{
    SYSTEMTIME ltime;
    GetLocalTime(&ltime);
    uint64_t retmilsec = ((ltime.wYear - 1970) * 365 + ((ltime.wYear - 1970) / 4 - 1) + day[ltime.wMonth - 1] + ltime.wDay - 1) * 86400 + ltime.wHour * 3600 + ltime.wMinute * 60 + ltime.wSecond;
    retmilsec = retmilsec * 1000 + ltime.wMilliseconds;
    return retmilsec;
}

 

int main()
{

    uint64_t starttime = GetTickCount64();
    for (int i = 0 ; i < 200000000;i++)
    {
        getCurrentTime1();
    }
    uint64_t endtime = GetTickCount64();
    cout<<"cost time(ms):\t" << endtime-starttime << "\t--->ftime" << endl;

    starttime = GetTickCount64();
    for (int i = 0; i < 200000000; i++)
    {
        getCurrentTime2();
    }
    endtime = GetTickCount64();
    cout<<"cost time(ms):\t" << endtime - starttime<<"\t--->GetTickCount64" << endl;

    starttime = GetTickCount64();
    for (int i = 0; i < 200000000; i++)
    {
        getCurrentTime3();
    }
    endtime = GetTickCount64();
    cout<<"cost time(ms):\t" << endtime - starttime << "\t--->GetSystemTimeAsFileTime" << endl;

    starttime = GetTickCount64();
    for (int i = 0; i < 200000000; i++)
    {
        getCurrentTime4();
    }
    endtime = GetTickCount64();
    cout<<"cost time(ms):\t" << endtime - starttime<<"\t--->QueryPerformanceCounter" << endl;

    starttime = GetTickCount64();
    for (int i = 0; i < 200000000; i++)
    {
        getCurrentTime5();
    }
    endtime = GetTickCount64();
    cout<< "cost time(ms):\t" << endtime - starttime << "\t--->GetLocalTime" << endl;

}

 

输出结果:

cost time(ms):  32230   --->ftime
cost time(ms):  6490    --->GetTickCount64
cost time(ms):  8128    --->GetSystemTimeAsFileTime
cost time(ms):  44226   --->QueryPerformanceCounter
cost time(ms):  28065   --->GetLocalTime
 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值