Elpezmuerto
Elpezmuerto

Reputation: 5571

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.

Upvotes: 0

Views: 3283

Answers (2)

Michał Trybus
Michał Trybus

Reputation: 11794

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.

Upvotes: 3

MarkD
MarkD

Reputation: 4954

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.

Upvotes: 3

Related Questions