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
 

 

 

性能分析是指对代码进行分析,以测量和评估程序运行时所需的时间和资源。而函数执行时间是指计算执行特定函数所需的时间。 要分析函数执行时间,我们可以使用一些工具和方法: 1. 使用编程语言或软件提供的内置函数来测量函数执行时间。大多数编程语言都提供了计时函数或类,例如Python的time模块的time()函数C++的clock()函数。通过在函数开始和结束的位置调用这些函数,并计算时间差,即可得到函数执行所需的时间。 2. 借助性能分析工具,如Google的Perf和Microsoft的Profiler等。这些工具可以帮助我们捕获和分析函数的执行时间,并提供更详细的性能指标和报告。可以使用这些工具来确定函数中的瓶颈和优化点。 3. 使用日志记录的方法进行性能分析。在函数中插入时间戳,并将其记录到日志文件中。然后,通过分析日志中的时间戳,可以计算函数执行时间。 在分析函数执行时间时,还应注意以下几点: 1. 多次执行函数以获取平均执行时间。单次的执行时间可能受到其他因素的影响,如操作系统的负载、硬件性能等。因此,最好多次执行函数,并计算这些执行时间的平均值。 2. 忽略初始化和清理时间函数的执行时间应该仅包括函数体内部的代码执行时间,而不包括函数调用前后的初始化和清理工作,以获取更准确的执行时间。 3. 考虑输入规模的影响。函数的执行时间可能随着输入规模的增加而增加或减少。因此,在性能分析时,应该对不同的输入规模进行测试,并比较它们之间的执行时间。 总而言之,通过使用合适的工具和方法,我们可以准确地分析函数的执行时间,并找到优化代码的方法,以提高程序的性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值