问题描述
我的程序中的一些加载例程需要很长时间才能完成。我想要一个快速的小片段,用于检查函数执行所花费的时间。
Some loading routines in my program takes to long to complete. I want a quick small snippet for checking how long a function took to execute. By small I mean "preferably without 3rd party libraries".
可能是像系统时间一样简单吗?
Maybe something as simple as taking the system time?
start = current_system_time()
load_something()
delta = current_system_time()-start
log_debug("load took "+delta)
修改:目标操作系统是Windows。
Target OS in question is Windows.
推荐答案
您的回答是:
注意:不会在多线程代码或多核心机器中工作,您需要一个强大的挂钟计时器。
所以我建议你使用omp的wallclock。 OMP包含在VC和GCC中,大多数编译器及其标准你不需要担心消失。
Caveat: That WON'T work in multihtreaded code or multiple core machines, you need a robust wall-clock timer.So I recommend you use omp's wallclock. OMP is included with VC and GCC, and most compilers and its a standard you don't need to worry about disappearing
#include <omp.h>
// Starting the time measurement
double start = omp_get_wtime();
// Computations to be measured
...
// Measuring the elapsed time
double end = omp_get_wtime();
// Time calculation (in seconds)
这篇关于寻找基准化代码段(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!