| 1 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|---|
| 2 | |
|---|
| 3 | /* test/timer.h |
|---|
| 4 | * Copyright (C) 1994-1997 Givaro Team |
|---|
| 5 | * |
|---|
| 6 | * Written by T. Gautier |
|---|
| 7 | * |
|---|
| 8 | * ------------------------------------ |
|---|
| 9 | * Modified by Bradford Hovinen <hovinen@cis.udel.edu> |
|---|
| 10 | * |
|---|
| 11 | * Added _start_t member to BaseTimer, so that stop () does not clobber the |
|---|
| 12 | * class' memory of its start time. This allows it to be called repeatedly to |
|---|
| 13 | * get elapsed times. |
|---|
| 14 | * ------------------------------------ |
|---|
| 15 | * Modified by Clement Pernet |
|---|
| 16 | * integrated into FFLAS_FFPACK |
|---|
| 17 | * |
|---|
| 18 | * ------------------------------------ |
|---|
| 19 | * See COPYING for license information. |
|---|
| 20 | * |
|---|
| 21 | * This file implements the C++ interface to commentators (for |
|---|
| 22 | * providing runtime commentary to the user) |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | #ifndef __TIMER_H |
|---|
| 26 | #define __TIMER_H |
|---|
| 27 | |
|---|
| 28 | #include <iostream> |
|---|
| 29 | |
|---|
| 30 | class BaseTimer { |
|---|
| 31 | public: |
|---|
| 32 | enum { |
|---|
| 33 | MSPSEC = 1000000 // microsecond per second |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | // -- Clear timer : |
|---|
| 37 | inline void clear() { _t = 0; } |
|---|
| 38 | |
|---|
| 39 | // -- total amount of second spent |
|---|
| 40 | inline double time() const { return _t; } |
|---|
| 41 | |
|---|
| 42 | // -- Return a value to initialize random generator |
|---|
| 43 | static long seed(); |
|---|
| 44 | |
|---|
| 45 | // -- basic methods: |
|---|
| 46 | std::ostream &print (std::ostream &) const; |
|---|
| 47 | |
|---|
| 48 | // -- Some arithmetic operators to compute cumulative time : |
|---|
| 49 | BaseTimer& operator = (const BaseTimer & T) ; |
|---|
| 50 | const BaseTimer operator - (const BaseTimer & T) const; |
|---|
| 51 | const BaseTimer operator - () ; |
|---|
| 52 | const BaseTimer operator + (const BaseTimer & T) const; |
|---|
| 53 | BaseTimer& operator += (const BaseTimer & T) { return *this = *this + T; }; |
|---|
| 54 | BaseTimer& operator -= (const BaseTimer & T) { return *this = *this - T; }; |
|---|
| 55 | |
|---|
| 56 | public: |
|---|
| 57 | double _start_t; // time as of start () |
|---|
| 58 | double _t; // time |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | inline std::ostream &operator << (std::ostream &o, const BaseTimer &BT) |
|---|
| 62 | { return BT.print(o); } |
|---|
| 63 | |
|---|
| 64 | class RealTimer : public BaseTimer { |
|---|
| 65 | public: |
|---|
| 66 | inline RealTimer (const BaseTimer &BT) : BaseTimer (BT) {}; |
|---|
| 67 | inline RealTimer () {}; |
|---|
| 68 | void start (); |
|---|
| 69 | void stop (); |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | class UserTimer : public BaseTimer { |
|---|
| 74 | public: |
|---|
| 75 | inline UserTimer (const BaseTimer &BT) : BaseTimer (BT) {}; |
|---|
| 76 | inline UserTimer () {}; |
|---|
| 77 | void start (); |
|---|
| 78 | void stop (); |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | class SysTimer : public BaseTimer { |
|---|
| 83 | public: |
|---|
| 84 | inline SysTimer (const BaseTimer &BT): BaseTimer (BT) {}; |
|---|
| 85 | inline SysTimer () {}; |
|---|
| 86 | void start (); |
|---|
| 87 | void stop (); |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | class Timer { |
|---|
| 92 | public : |
|---|
| 93 | |
|---|
| 94 | // Clear timer : |
|---|
| 95 | void clear(); |
|---|
| 96 | |
|---|
| 97 | // Start timer |
|---|
| 98 | void start (); |
|---|
| 99 | |
|---|
| 100 | // Stop timer |
|---|
| 101 | void stop (); |
|---|
| 102 | |
|---|
| 103 | // total amount of second spent in user mode |
|---|
| 104 | double usertime () const { return ut.time(); } |
|---|
| 105 | |
|---|
| 106 | // total amount of second spent in system mode |
|---|
| 107 | double systime () const { return st.time(); } |
|---|
| 108 | |
|---|
| 109 | // real total amount of second spent. |
|---|
| 110 | double realtime () const { return rt.time(); } |
|---|
| 111 | |
|---|
| 112 | // retourne une petite graine |
|---|
| 113 | // long seed() const { return RealTimer::seed(); } |
|---|
| 114 | |
|---|
| 115 | // Some arithmetic operators to compute cumulative time : |
|---|
| 116 | Timer& operator = (const Timer & T) ; |
|---|
| 117 | const Timer operator - (const Timer & T) const; |
|---|
| 118 | const Timer operator - () ; |
|---|
| 119 | const Timer operator + (const Timer & T) const; |
|---|
| 120 | Timer& operator += (const Timer & T) { return *this = *this + T; }; |
|---|
| 121 | Timer& operator -= (const Timer & T) { return *this = *this - T; }; |
|---|
| 122 | |
|---|
| 123 | // -- methods : |
|---|
| 124 | std::ostream &print (std::ostream &) const; |
|---|
| 125 | |
|---|
| 126 | size_t count() const {return _count;} |
|---|
| 127 | |
|---|
| 128 | private: |
|---|
| 129 | size_t _count; // how many |
|---|
| 130 | |
|---|
| 131 | RealTimer rt; |
|---|
| 132 | UserTimer ut; |
|---|
| 133 | SysTimer st; |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | // inline std::ostream &operator << (std::ostream &o, const Timer &T) |
|---|
| 137 | // { return T.print (o); } |
|---|
| 138 | |
|---|
| 139 | inline std::ostream &operator << (std::ostream &o, const Timer &T) |
|---|
| 140 | { |
|---|
| 141 | double ut = T.usertime(); |
|---|
| 142 | if (ut < 0.0000000001) ut = 0; |
|---|
| 143 | return o << T.realtime() << "s (" << ut << " cpu) [" << T.count() << "]"; } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | #include "timer.C" |
|---|
| 148 | |
|---|
| 149 | #endif |
|---|