PDA

Click to See Complete Forum and Search --> : elapsed time?


Dillinger4
Jun 18th, 2000, 07:26 AM
Does any one knoe how i can calculate elapsed time?

This is the crap that microsoft has included in their
MSDN Library:

CTime startTime = CTime::GetCurrentTime();

// ... perform time-consuming task ...

CTime endTime = CTime::GetCurrentTime();

CTimeSpan elapsedTime = endTime - startTime;

They say that you have to include #include <afx.h>.
So are Ctime & CTimeSpan, classes that are already defined
in the afx header file? Im trying to calculate feet travled
based on current speed. but i have no clue how to do this.

do while(cur_speed > 0)
{
feet_travled = cur_speed * seconds() * 1.46;
}

thanks

parksie
Jun 19th, 2000, 04:36 AM
don't use the afx functions for this...


#include <time.h>

void myfunc() {
time_t start;
time_t end;
time_t elapsed;
float speed, distance;

start = time();

// task here

end = time();
elapsed = (end - start);

// elapsed is now in clock units (usually ms)

elapsed /= CLOCKS_PER_SEC;

// elapsed is in seconds

speed = distance / elapsed;
}


the time.h header is standard and will work whatever. (I'm assuming SI units for calculations). CTime and CTimeSpan are MFC classes.

to use something useful, you need to do something like keep track of the time, then every 2 seconds, find the distance travelled in the past 2 secs to find the speed. you can't really work out the time over 0 secs because you'd have to differentiate (nasty).