Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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

The COleDateTime is known to internally work based on milliseconds, because it stores timestamp on its m_dt variable, which is of DATE type, so having resolution for the intended purpose.

I can suggest you to base your time on

DATE now= (DATE) COleDateTime::GetCurrentTime();

and after do the respective calculations.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top