| 1 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|---|
| 2 | |
|---|
| 3 | /* linbox/util/timer.C |
|---|
| 4 | * Copyright (C) 1994-1997 Givaro Team |
|---|
| 5 | * |
|---|
| 6 | * Written by T. Gautier |
|---|
| 7 | * |
|---|
| 8 | * This library is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU Lesser General Public |
|---|
| 10 | * License as published by the Free Software Foundation; either |
|---|
| 11 | * version 2 of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This library is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | * Lesser General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU Lesser General Public |
|---|
| 19 | * License along with this library; if not, write to the |
|---|
| 20 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|---|
| 21 | * Boston, MA 02111-1307, USA. |
|---|
| 22 | * |
|---|
| 23 | * This file implements the C++ interface to commentators (for |
|---|
| 24 | * providing runtime commentary to the user) |
|---|
| 25 | */ |
|---|
| 26 | #ifndef __LINBOX__TIMER__C__ |
|---|
| 27 | #define __LINBOX__TIMER__C__ |
|---|
| 28 | // Description: |
|---|
| 29 | // - various timer objects |
|---|
| 30 | // - to be rewritten to be more efficient |
|---|
| 31 | |
|---|
| 32 | #include <cmath> |
|---|
| 33 | |
|---|
| 34 | extern "C" { |
|---|
| 35 | # include <sys/time.h> |
|---|
| 36 | # include <sys/resource.h> |
|---|
| 37 | // int getrusage (int, struct rusage*) ; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | #include <iostream> |
|---|
| 41 | |
|---|
| 42 | #include "timer.h" |
|---|
| 43 | |
|---|
| 44 | // Return a value to initialize random generator |
|---|
| 45 | long BaseTimer::seed() |
|---|
| 46 | { |
|---|
| 47 | struct timeval tp; |
|---|
| 48 | gettimeofday(&tp, 0) ; |
|---|
| 49 | return(tp.tv_usec); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | // Output the value of the timer : |
|---|
| 53 | std::ostream& BaseTimer::print( std::ostream& o ) const |
|---|
| 54 | { return o << _t ; } |
|---|
| 55 | |
|---|
| 56 | // Some arithmetic operator : |
|---|
| 57 | BaseTimer& BaseTimer::operator = (const BaseTimer & T) |
|---|
| 58 | { |
|---|
| 59 | _t = T._t ; |
|---|
| 60 | return *this ; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | // Computes and returns interval of time |
|---|
| 64 | // beteween *this and T |
|---|
| 65 | const BaseTimer BaseTimer::operator - (const BaseTimer & T) const |
|---|
| 66 | { |
|---|
| 67 | BaseTimer Tmp ; |
|---|
| 68 | Tmp._t = _t - T._t ; |
|---|
| 69 | return Tmp ; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | const BaseTimer BaseTimer::operator - () |
|---|
| 73 | { |
|---|
| 74 | BaseTimer Tmp ; |
|---|
| 75 | Tmp._t = -_t ; |
|---|
| 76 | return Tmp ; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | const BaseTimer BaseTimer::operator + (const BaseTimer & T) const |
|---|
| 80 | { |
|---|
| 81 | BaseTimer Tmp ; |
|---|
| 82 | Tmp._t = _t + T._t ; |
|---|
| 83 | return Tmp ; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // Start timer |
|---|
| 87 | void RealTimer::start() |
|---|
| 88 | { |
|---|
| 89 | struct timeval tmp2 ; |
|---|
| 90 | gettimeofday (&tmp2, 0) ; |
|---|
| 91 | |
|---|
| 92 | // real time |
|---|
| 93 | _start_t = (double) tmp2.tv_sec + |
|---|
| 94 | ((double) tmp2.tv_usec)/ (double)BaseTimer::MSPSEC ; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | // Stop timer |
|---|
| 99 | void RealTimer::stop() |
|---|
| 100 | { |
|---|
| 101 | struct timeval tmp2 ; |
|---|
| 102 | gettimeofday (&tmp2, 0) ; |
|---|
| 103 | |
|---|
| 104 | // real time |
|---|
| 105 | _t = (double) tmp2.tv_sec + |
|---|
| 106 | ((double) tmp2.tv_usec)/ (double)BaseTimer::MSPSEC - _start_t ; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // Start timer |
|---|
| 110 | void UserTimer::start() |
|---|
| 111 | { |
|---|
| 112 | struct rusage tmp1 ; // to getrusage (sys+user times) |
|---|
| 113 | getrusage (RUSAGE_SELF, &tmp1) ; |
|---|
| 114 | // user time |
|---|
| 115 | _start_t = (double) tmp1.ru_utime.tv_sec + |
|---|
| 116 | ((double) tmp1.ru_utime.tv_usec)/ (double)MSPSEC ; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | // Stop timer |
|---|
| 121 | void UserTimer::stop() |
|---|
| 122 | { |
|---|
| 123 | struct rusage tmp1 ; // to getrusage (sys+user times) |
|---|
| 124 | getrusage (RUSAGE_SELF, &tmp1) ; |
|---|
| 125 | // user time |
|---|
| 126 | _t = (double) tmp1.ru_utime.tv_sec + |
|---|
| 127 | ((double) tmp1.ru_utime.tv_usec)/ (double)MSPSEC - _start_t ; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | // Start timer |
|---|
| 132 | void SysTimer::start() |
|---|
| 133 | { |
|---|
| 134 | struct rusage tmp1 ; // to getrusage (sys+user times) |
|---|
| 135 | getrusage (RUSAGE_SELF, &tmp1) ; |
|---|
| 136 | // user time |
|---|
| 137 | _start_t = (double) tmp1.ru_stime.tv_sec + |
|---|
| 138 | ((double) tmp1.ru_stime.tv_usec)/ (double)MSPSEC ; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | // Stop timer |
|---|
| 143 | void SysTimer::stop() |
|---|
| 144 | { |
|---|
| 145 | struct rusage tmp1 ; // to getrusage (sys+user times) |
|---|
| 146 | getrusage (RUSAGE_SELF, &tmp1) ; |
|---|
| 147 | // user time |
|---|
| 148 | _t = (double) tmp1.ru_stime.tv_sec + |
|---|
| 149 | ((double) tmp1.ru_stime.tv_usec)/ (double)MSPSEC - _start_t ; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | // Clear timer : |
|---|
| 155 | void Timer::clear() |
|---|
| 156 | { rt.clear() ; ut.clear(); st.clear(); _count = 0; } |
|---|
| 157 | |
|---|
| 158 | // Start timer |
|---|
| 159 | void Timer::start() |
|---|
| 160 | { rt.start() ; ut.start(); st.start(); _count = 0; } |
|---|
| 161 | |
|---|
| 162 | // Stop timer |
|---|
| 163 | void Timer::stop() |
|---|
| 164 | { rt.stop() ; ut.stop(); st.stop(); _count = 1; } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | std::ostream& Timer::print( std::ostream& o ) const |
|---|
| 168 | { |
|---|
| 169 | o << "user time: " << usertime() << '\n' ; |
|---|
| 170 | o << "sys. time: " << systime() << '\n' ; |
|---|
| 171 | return o << "real time: " << realtime() << std::endl ; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | // Some arithmetic operator : |
|---|
| 175 | Timer& Timer::operator = (const Timer & T) |
|---|
| 176 | { |
|---|
| 177 | ut = T.ut ; |
|---|
| 178 | st = T.st ; |
|---|
| 179 | rt = T.rt ; |
|---|
| 180 | _count = T._count; |
|---|
| 181 | return *this ; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | // Comput._tes and returns interval of time |
|---|
| 185 | // beteween *this and T |
|---|
| 186 | const Timer Timer::operator - (const Timer & T) const |
|---|
| 187 | { |
|---|
| 188 | Timer Tmp ; |
|---|
| 189 | Tmp.ut = ut - T.ut ; |
|---|
| 190 | Tmp.st = st - T.st ; |
|---|
| 191 | Tmp.rt = rt - T.rt ; |
|---|
| 192 | Tmp._count = _count - T._count; |
|---|
| 193 | return Tmp ; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | const Timer Timer::operator - () |
|---|
| 197 | { |
|---|
| 198 | Timer Tmp ; |
|---|
| 199 | Tmp.ut = -ut ; |
|---|
| 200 | Tmp.st = -st ; |
|---|
| 201 | Tmp.rt = -rt ; |
|---|
| 202 | Tmp._count = - _count; |
|---|
| 203 | return Tmp ; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | const Timer Timer::operator + (const Timer & T) const |
|---|
| 207 | { |
|---|
| 208 | Timer Tmp ; |
|---|
| 209 | Tmp.ut = ut + T.ut ; |
|---|
| 210 | Tmp.st = st + T.st ; |
|---|
| 211 | Tmp.rt = rt + T.rt ; |
|---|
| 212 | Tmp._count = _count + T._count; |
|---|
| 213 | return Tmp ; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | #endif |
|---|