简体   繁体   中英

Calculate Execution TIme using C++, rather than using <time.h>

I need to calculate the execution time of a C++ program. I am currently using the c-style head, <time.h> with the following code:

clock_t tStart = clock();
...
printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);

Is there a c++ header I can use to calculate execution time? Please note this needs to be cross-platform compatible.

The time and clock functions are one of those things that vary from platform to platform.

I like to use Boost's timer library for what you are trying to do: http://www.boost.org/doc/libs/1_43_0/libs/timer/timer.htm as it works well cross platform, and is quite straightforward to use.

You can use the C++ versions of C headers. Add 'c' at the beginning, and remove '.h'

So you need

#include <ctime>

The rest stays the same, as you can just use the C approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM