c++ - 线程 : How to calculate precisely the execution time of an algorithm (duration of function) in C or C++?

标签 c++ c multithreading execution-time

有一种简单的方法可以计算此处描述的任何函数的持续时间:How to Calculate Execution Time of a Code Snippet in C++

start_timestamp = get_current_uptime();
// measured algorithm
duration_of_code = get_current_uptime() - start_timestamp;

但是,它不允许获得明确的持续时间,因为其他线程的执行时间将包含在测量时间中。

所以问题是:如何考虑代码在其他线程中花费的时间? OSX 代码偏好。虽然查看 Windows 或 Linux 代码也很棒...

更新:理想吗?代码概念

start_timestamp = get_this_thread_current_uptime();
// measured algorithm
duration_of_code = get_this_thread_current_uptime() - start_timestamp;

最佳答案

我很遗憾地说,在一般情况下,没有办法做你想做的事。您正在寻找worst-case execution time ,并且有几种方法可以对此进行良好的近似,但是没有完美的方法,因为 WCET 相当于 Halting problem .

关于c++ - 线程 : How to calculate precisely the execution time of an algorithm (duration of function) in C or C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070994/

相关文章:

java - visual c++ 将 java 类作为进程运行

c++ - 在进程外调用时 MiniDumpWriteDump 中的访问冲突

c - C程序中未初始化内存位置的物理内存位置?

python - 在 Windows 上测量每个线程的 CPU 时间

c++ - 在此流运算符重载中由 std::ostream::sentry 引起的段错误

c++ - STL 自定义分配器

c - 如何在C中复制多维字符串

C 传递和指针

c# - 关于任务延续的问题

java - 如何暂停一个线程并在它停止的地方恢复它