Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFC measure function in milliseconds

Tags:

mfc

ctime

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.

like image 281
ilansch Avatar asked Dec 25 '12 11:12

ilansch


2 Answers

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.

like image 82
Tom van der Woerdt Avatar answered Sep 30 '22 09:09

Tom van der Woerdt


If you are using MFC, the nice way is to use WIN API. And since you are worried just to calculate the time difference, the below function might suit you perfectly.

GetTickCount64()

directly returns the number of milli seconds that has elapsed since the system was started.

If you don't plan to keep your system up long (precisely more than 49.7 days), little bit faster version - GetTickCount() function

like image 26
Mohan Avatar answered Sep 30 '22 09:09

Mohan