LCOV - code coverage report
Current view: top level - tools - Stopwatch.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 10 10 100.0 %
Date: 2018-12-19 07:49:13 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2018 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #ifndef __PLUMED_tools_Stopwatch_h
      23             : #define __PLUMED_tools_Stopwatch_h
      24             : 
      25             : #include <string>
      26             : #include <map>
      27             : #include <iosfwd>
      28             : 
      29             : namespace PLMD {
      30             : 
      31             : /**
      32             : \ingroup TOOLBOX
      33             : Class implementing stopwatch to time execution.
      34             : 
      35             : Each instance of this class is a container which
      36             : can keep track of several named stopwatches at
      37             : the same time. Access to the stopwatches
      38             : is obtained using start(), stop(), pause() methods,
      39             : giving as a parameter the name of the specific stopwatch.
      40             : Also an empty string can be used (un-named stopwatch).
      41             : Finally, all the times can be logged using << operator
      42             : 
      43             : \verbatim
      44             : #include "Stopwatch.h"
      45             : 
      46             : int main(){
      47             :   Stopwatch sw;
      48             :   sw.start();
      49             : 
      50             :   sw.start("initialization");
      51             : // do initialization ...
      52             :   sw.stop("initialization");
      53             : 
      54             :   for(int i=0;i<100;i++){
      55             :     sw.start("loop");
      56             : // do calculation
      57             :     sw.stop("loop");
      58             :   }
      59             : 
      60             :   sw.stop();
      61             :   return 0;
      62             : }
      63             : 
      64             : \endverbatim
      65             : 
      66             : Using pause a stopwatch can be put on hold until
      67             : the next start:
      68             : 
      69             : \verbatim
      70             : #include "Stopwatch.h"
      71             : 
      72             : int main(){
      73             :   Stopwatch sw;
      74             :   sw.start();
      75             : 
      76             :   sw.start("initialization");
      77             : // do initialization ...
      78             :   sw.stop("initialization");
      79             : 
      80             :   for(int i=0;i<100;i++){
      81             :     sw.start("loop");
      82             : // do calculation
      83             :     sw.pause("loop");
      84             : // here goes something that we do not want to include
      85             :     sw.start("loop");
      86             : // do calculation
      87             :     sw.stop("loop");
      88             :   }
      89             : 
      90             :   sw.stop();
      91             :   return 0;
      92             : }
      93             : 
      94             : \endverbatim
      95             : 
      96             : */
      97             : 
      98        3014 : class Stopwatch {
      99             : /// Class to hold the value of absolute time
     100             :   class Time {
     101             :   public:
     102             :     unsigned long sec;
     103             : /// I store nanosecond so as to allow high resolution clocks
     104             : /// (even if likely time will be measured in microseconds)
     105             :     unsigned      nsec;
     106             :     Time();
     107             :     Time operator-(const Time&)const;
     108             :     const Time & operator+=(const Time&);
     109             :     operator double()const;
     110             :     static Time get();
     111             :     void reset();
     112             :   };
     113             : /// Class to store a single stopwatch.
     114             : /// Class Stopwatch contains a collection of them
     115             :   class Watch {
     116             :   public:
     117             :     Watch();
     118             :     Time total;
     119             :     Time lastStart;
     120             :     Time lap;
     121             :     Time max;
     122             :     Time min;
     123             :     unsigned cycles;
     124             :     unsigned running;
     125             :     void start();
     126             :     void stop();
     127             :     void pause();
     128             :   };
     129             :   std::map<std::string,Watch> watches;
     130             :   std::ostream& log(std::ostream&)const;
     131             : public:
     132             : /// Start timer named "name"
     133             :   void start(const std::string&name);
     134             :   void start();
     135             : /// Stop timer named "name"
     136             :   void stop(const std::string&name);
     137             :   void stop();
     138             : /// Pause timer named "name"
     139             :   void pause(const std::string&name);
     140             :   void pause();
     141             : /// Dump all timers on an ostream
     142             :   friend std::ostream& operator<<(std::ostream&,const Stopwatch&);
     143             : };
     144             : 
     145             : inline
     146      198293 : void Stopwatch::start() {
     147      198293 :   start("");
     148      198293 : }
     149             : 
     150             : inline
     151        1500 : void Stopwatch::stop() {
     152        1500 :   stop("");
     153        1500 : }
     154             : 
     155             : inline
     156      196793 : void Stopwatch::pause() {
     157      196793 :   pause("");
     158      196793 : }
     159             : 
     160             : }
     161             : 
     162             : 
     163             : #endif

Generated by: LCOV version 1.13