diff --git a/cmTime.c b/cmTime.c index fe9a0fa..5c7db8b 100644 --- a/cmTime.c +++ b/cmTime.c @@ -59,3 +59,15 @@ unsigned cmTimeElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) // take diff between t1 and t0 return u1 - u0; } + +unsigned cmTimeAbsElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) +{ + if( t1->tv_sec > t0->tv_sec ) + return cmTimeElapsedMicros(t0,t1); + + if( t1->tv_sec == t0->tv_sec ) + if( t1->tv_nsec > t0->tv_nsec ) + return cmTimeElapsedMicros(t0,t1); + + return cmTimeElapsedMicros(t1,t0); +} diff --git a/cmTime.h b/cmTime.h index 6bad0af..a5aa639 100644 --- a/cmTime.h +++ b/cmTime.h @@ -24,15 +24,14 @@ extern "C" { */ void cmTimeGet( cmTimeSpec_t* t ); - // Return the elapsed time (t1 - t0) - // in microseconds - unsigned cmTimeElapsedMicros - ( - const - cmTimeSpec_t* - t0, //< ptr to start time - const cmTimeSpec_t* t1 );// ptr to end time + // Return the elapsed time (t1 - t0) in microseconds + // t1 is assumed to be at a later time than t0. + unsigned cmTimeElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); + + // Same as cmTimeElapsedMicros() but the times are not assumed to be ordered. + // The function therefore begins by swapping t1 and t0 if t0 is after t1. + unsigned cmTimeAbsElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); //) //}