| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef PROCTIMER_H |
| 21 | #define PROCTIMER_H |
| 22 | |
| 23 | #include <ctime> |
| 24 | |
| 25 | /** |
| 26 | * Class TsProcTimer definition & implementation |
| 27 | * |
| 28 | * Useful for calculating processor time intervals up to 10^-3 sec |
| 29 | * |
| 30 | */ |
| 31 | class TsProcTimer |
| 32 | { |
| 33 | private: // members |
| 34 | /// save the starting time of the timer |
| 35 | clock_t startTime = 0; |
| 36 | /// calculated time between Start() and Stop() calls |
| 37 | float resultTime = 0.0f; |
| 38 | /// flag to show timer is started |
| 39 | bool Started = false; |
| 40 | |
| 41 | private: // methods |
| 42 | /// get time interval between startTime and current time |
| 43 | float calcDelta ( void ) const; |
| 44 | |
| 45 | public: // interface |
| 46 | /// reset timer |
| 47 | void Reset ( void ); |
| 48 | |
| 49 | /// record current time |
| 50 | void Start ( void ); |
| 51 | /// save time interval from starting point to current moment |
| 52 | void Stop ( void ); |
| 53 | |
| 54 | /// get time interval |
| 55 | operator float ( void ) const; |
| 56 | }; // TsProcTimer |
| 57 | |
| 58 | /** |
| 59 | * TsTimer implementation |
| 60 | */ |
| 61 | |
| 62 | inline void TsProcTimer :: Reset ( void ) |
| 63 | { |
| 64 | Started = false; |
| 65 | resultTime = 0; |
| 66 | } |
| 67 | |
| 68 | inline float TsProcTimer :: calcDelta ( void ) const |
| 69 | { |
| 70 | clock_t finishTime = clock(); |
| 71 | |
| 72 | // calculate difference between current time and start time |
| 73 | float realProcTime = float(finishTime>=startTime ? |
| 74 | finishTime-startTime : |
| 75 | ((clock_t)-1)-startTime+finishTime ) / CLOCKS_PER_SEC; |
| 76 | |
| 77 | // correct times less than a millisecond |
| 78 | if ( realProcTime < 1e-3 ) |
| 79 | realProcTime = 0; |
| 80 | |
| 81 | return realProcTime; |
| 82 | } |
| 83 | |
| 84 | inline TsProcTimer :: operator float ( void ) const |
| 85 | { |
| 86 | const unsigned int norm = 100; // normalisation constant |
| 87 | float realProcTime; |
| 88 | |
| 89 | if ( Started ) |
| 90 | realProcTime = calcDelta(); |
| 91 | else |
| 92 | realProcTime = resultTime; |
| 93 | |
| 94 | // normalise value up to CONST |
| 95 | return ((unsigned long)(realProcTime*norm))/(float)norm; |
| 96 | } |
| 97 | |
| 98 | inline void TsProcTimer :: Start ( void ) |
| 99 | { |
| 100 | if ( !Started ) |
| 101 | { |
| 102 | startTime = clock(); |
| 103 | Started = true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | inline void TsProcTimer :: Stop ( void ) |
| 108 | { |
| 109 | if ( Started ) |
| 110 | { |
| 111 | Started = false; |
| 112 | resultTime += calcDelta (); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #endif |