Boost logo

Boost :

From: sipan_at_[hidden]
Date: 2000-04-09 20:01:12


Hi all,

I am new to the list, so please forgive we if I am restarting
some old discussion I am not aware of.

> I am little confused by your approaches to measuring time. It seems
> that your options are to use unprecise clock() or some exotic
> timers.

Same here!

1) Do you want to measure process time or actual elaped time?
2) Why you call it timer, not stopwatch?
   /* For me, timer is something more then a stop watch -- in
   mutithreaded env. one might need an object which can register
   multiple timeouts with callbacks (AKA as signals attached to
   slots) and that what I would call timer. */

> I wrote the attached class some time ago for my own purposes,
> and it worked beautifully.It is a Win32 only implementation,

I wrote StopWatch that measures elapsed time on Linux(it is less
accurate on NT). It uses gettimeofday function (SVr4, BSD). It is
not a submittion -- not finished, wrong style, etc., It is just a
suggestion how to get usec resolution on UNIX platforms.

Sergey
==========================8<--------------------------
#include <sys/time.h>

class StopWatch {

#ifdef OS_WIN32
        typedef DWORD Time;
#elif OS_LINUX
        typedef struct timeval Time;
#endif /* OS_LINUX */

        void get_time(Time* T);
        ulong interval_mcsec(Time start, Time end);

        Time start;
        Time current;

public:

        StopWatch();
        
        // get elapsed time in usec and reset stop-watch
        ulong reset ();

        // get elapsed time in usec
        ulong get_time ();
        
};

inline StopWatch::StopWatch() {cm_get_time(&start);}
        
inline ulong StopWatch::reset ()
{
        u64 ret_val;
        get_time(&current);
        ret_val = interval_usec(start,current);
        get_time(&start);
        return ret_val;
}
        
inline ulong StopWatch::get_time ()
{
        get_time(&current);
        return interval_usec(start,current);
}

#ifdef OS_WIN32 /* ------ Windows ------- */

inline void StopWatch::get_time(StopWatch::Time* t)
{
        *(t) = GetTickCount();
}

inline ulong StopWatch::interval_mcsec(StopWatch::Time start,
                                       StopWatch::Time end)
{
        return (end < start) ?
                (UINT_MAX - (start - end))*1000 :
                (end - start)*1000;
}

#elif OS_LINUX /* --------- Linux (other UNIX-es?)-------------- */

#define CM_MICROSEC 1000000

inline void StopWatch::get_time(StopWatch::Time* t)
{
        gettimeofday(t,NULL);
}

inline uint32 StopWatch::interval_mcsec(StopWatch::Time start,
                                        StopWatch::Time end)
{
        return (end.tv_usec < start.tv_usec) ?
                ( end.tv_sec - start.tv_sec)*CM_MICROSEC
                -
                ( start.tv_usec - end.tv_usec)
                :
                (end.tv_sec - start.tv_sec)*CM_MICROSEC
                +
                (end.tv_usec - start.tv_usec);
}

#undef CM_MICROSEC

#endif /*
-------------------------------------------------------------- */


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk