Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Timer.H

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Wed Nov 27 12:50:07 EST 2002 by ken@xorian.net
00020 //      modified on Fri Jul 19 09:45:56 PDT 1996 by heydon
00021 
00022 /* A "Timer::T" is an object for measuring short elapsed times. It reports
00023    times in milliseconds.
00024 
00025    A timer object is in one of two states: stopped or running. Initially, a
00026    timer is stopped. There are methods for starting and stopping the timer.
00027    Whenever a timer is stopped, the time it had been running is added to its
00028    total elapsed time. A timer may be started and stopped multiple times; the
00029    total elapsed time accumulates the running time of each interval. The
00030    "Reset" method can be used to set the total elapsed time to 0.
00031 
00032    The methods of a "Timer::T" are not thread-safe. */
00033 
00034 #ifndef _TIMER_H
00035 #define _TIMER_H
00036 
00037 #include <sys/time.h>
00038 #include <Basics.H>
00039 
00040 class Timer {
00041   public:
00042     // A "Timer::T" measures time in microseconds
00043     typedef Basics::uint64 MicroSecs;
00044 
00045     // the granularity of the timer
00046     static MicroSecs Grain; // read-only after initialization
00047 
00048     class T {
00049       public:
00050         T() throw () : running(false), elapsed(0UL) { /* SKIP */ }
00051         /* Create a new timer. Initially, the timer is not running and
00052            has an elapsed time of 0 usecs. */
00053 
00054         void Start() throw ();
00055         /* Start the timer. A checked run-time error occurs if the timer is
00056            already running. */
00057 
00058         MicroSecs Stop() throw ();
00059         /* Stop the timer, update the total elapsed time, and return the
00060            total time since the previous "Start" operation (in usecs).
00061            The reported time will be either 0 or >= "Grain". A checked
00062            run-time error occurs if the timer is not running. */
00063 
00064         MicroSecs Reset() throw ();
00065         /* Return the total elapsed time of the timer and reset that elapsed
00066            time to 0 usecs. A checked run-time error occurs if the timer is
00067            running. */
00068 
00069         MicroSecs Elapsed() const throw ()
00070             { assert(!this->running); return this->elapsed; }
00071         /* Report the elapsed time of the timer. A checked run-time error
00072            occurs if the timer is running. */
00073 
00074       private:
00075         bool running;             // is the timer running?
00076         MicroSecs elapsed;        // total elapsed time
00077         struct timeval startTime; // starting time of last "Start" operation
00078     };
00079 };
00080 
00081 #endif // _TIMER_H

Generated on Mon May 8 00:48:33 2006 for Vesta by  doxygen 1.4.2