c++ - 快速时间函数 C/C++

标签 c++ c performance time

我目前正在使用 ctime 库中的时间。有没有更快的选择?

time_t start_time, elapsed_time;

for(int i = 0; i < n; i++) {
    start_time = time(NULL);
    /// optimized code
    if(condition_met())
    {
       elapsed_time = time(NULL) - start_time;
    } else continue;
}

time(NULL) 还不够快。

最佳答案

您似乎只想测量耗时(而不关心绝对时间)。测量耗时的最快方法之一(如果您使用的是 x86)是阅读 rdtsc counter .在 mvsc++ 中,这可以通过以下方式实现:

#include <intrin.h>
unsigned __int64 rdtsc(void)
{
    return __rdtsc();
}

关于c++ - 快速时间函数 C/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12004286/

相关文章:

c++ - multilist数据结构在C++中的应用

c - 头文件出现意外的文件结束符

angularjs - AngularJS 中的 parentValueWatch 是什么?

c++ - 仅使用 DLL *.h 头文件构建(编译链接)应用程序代码并在运行时加载 DLL 实现(显式链接)

c++ - 将 WindowClass 更改为全屏

计算字符串中相对于字符串长度的字母频率的 C 程序

node.js - 简单的 hello-world 应用程序在加载 800 个并发请求时速度很慢(130+ms)

wpf - 如何知道为什么动画会口吃?

c++ - OSX、VideoToolbox、压缩 session

c - C中是否允许负数组索引?