| 48 | }; |
| 49 | |
| 50 | class Timer |
| 51 | // A singleton class which maintains a collection of named timers. |
| 52 | // (A class is called a "singleton" if at most one instance of it |
| 53 | // may ever exist.) The following routines are provided: |
| 54 | // |
| 55 | // - start(name): If this is the first time start() has been |
| 56 | // called with name as the first parameter, then a new timer is |
| 57 | // created and started. Otherwise, the timer with name name is |
| 58 | // restarted. |
| 59 | // |
| 60 | // - lapse(name): Returns the number of seconds which have elapsed |
| 61 | // since start(name) was called last. |
| 62 | // Precondition: start(name) has been called once. |
| 63 | { |
| 64 | private: // (Construction and destruction are private to prevent |
| 65 | // more than one instantiation.) |
| 66 | |
| 67 | Timer(); |
| 68 | Timer(const Timer&); |
| 69 | Timer& operator=(const Timer&); |
| 70 | |
| 71 | public: // access and routines: |
| 72 | |
| 73 | static Timer& instance(); |
| 74 | // Returns a reference to the only existing instance of this class: |
| 75 | |
| 76 | void start(const char *name); |
| 77 | float lapse(const char *name); |
| 78 | |
| 79 | private: // private members: |
| 80 | typedef std::map<std::string,timeval> Timers; |
| 81 | Timers timers; // a collection of pairs (k,v) where |
| 82 | // k is the timer name and v is the |
| 83 | // (started) timer associated with k |
| 84 | }; |
| 85 | |
| 86 | } // namespace SEB_NAMESPACE |
| 87 | |