有一种简单的方法可以计算此处描述的任何函数的持续时间: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/