MFC measure function in milliseconds

ilansch picture ilansch · Dec 25, 2012 · Viewed 10k times · Source

How can i count the millisecond a certain function (called repeatedly) takes ?
I thought of:
CTime::GetCurrentTM() before,
CTime::GetCurrentTM() after,

And then insert the result to CTimeSpan diff = after - before.
Finally store that diff to global member that sum all diffs since i want to know the total time this function spent.

but it will give the answer in seconds and not milliseconds.

Answer

Tom van der Woerdt picture Tom van der Woerdt · Dec 25, 2012

MFC is C++, right?

If so, you can just use clock().

#include <ctime>

clock_t time1 = clock();
// do something heavy
clock_t time2 = clock();
clock_t timediff = time2 - time1;

float timediff_sec = ((float)timediff) / CLOCKS_PER_SEC;

This will usually give you millisecond precision.