LCOV - code coverage report
Current view: top level - tools - Stopwatch.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 11 11 100.0 %
Date: 2020-11-18 11:20:57 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2019 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 <unordered_map>
      27             : #include <iosfwd>
      28             : #include <chrono>
      29             : 
      30             : namespace PLMD {
      31             : 
      32             : /**
      33             : \ingroup TOOLBOX
      34             : Class implementing stopwatch to time execution.
      35             : 
      36             : Each instance of this class is a container which
      37             : can keep track of several named stopwatches at
      38             : the same time. Access to the stopwatches
      39             : is obtained using start(), stop(), pause() methods,
      40             : giving as a parameter the name of the specific stopwatch.
      41             : Also an empty string can be used (un-named stopwatch).
      42             : Finally, all the times can be logged using << operator
      43             : 
      44             : \verbatim
      45             : #include "Stopwatch.h"
      46             : 
      47             : int main(){
      48             :   Stopwatch sw;
      49             :   sw.start();
      50             : 
      51             :   sw.start("initialization");
      52             : // do initialization ...
      53             :   sw.stop("initialization");
      54             : 
      55             :   for(int i=0;i<100;i++){
      56             :     sw.start("loop");
      57             : // do calculation
      58             :     sw.stop("loop");
      59             :   }
      60             : 
      61             :   sw.stop();
      62             :   return 0;
      63             : }
      64             : 
      65             : \endverbatim
      66             : 
      67             : Using pause a stopwatch can be put on hold until
      68             : the next start:
      69             : 
      70             : \verbatim
      71             : #include "Stopwatch.h"
      72             : 
      73             : int main(){
      74             :   Stopwatch sw;
      75             :   sw.start();
      76             : 
      77             :   sw.start("initialization");
      78             : // do initialization ...
      79             :   sw.stop("initialization");
      80             : 
      81             :   for(int i=0;i<100;i++){
      82             :     sw.start("loop");
      83             : // do calculation
      84             :     sw.pause("loop");
      85             : // here goes something that we do not want to include
      86             :     sw.start("loop");
      87             : // do calculation
      88             :     sw.stop("loop");
      89             :   }
      90             : 
      91             :   sw.stop();
      92             :   return 0;
      93             : }
      94             : 
      95             : \endverbatim
      96             : 
      97             : */
      98             : 
      99        2662 : class Stopwatch {
     100             : /// Class to store a single stopwatch.
     101             : /// Class Stopwatch contains a collection of them
     102        6206 :   class Watch {
     103             :   public:
     104             :     std::chrono::time_point<std::chrono::high_resolution_clock> lastStart;
     105             :     long long int total = 0;
     106             :     long long int lap = 0;
     107             :     long long int max = 0;
     108             :     long long int min = 0;
     109             :     unsigned cycles = 0;
     110             :     unsigned running = 0;
     111             :     void start();
     112             :     void stop();
     113             :     void pause();
     114             :   };
     115             :   std::unordered_map<std::string,Watch> watches;
     116             :   std::ostream& log(std::ostream&)const;
     117             : public:
     118             : /// Start timer named "name"
     119             :   void start(const std::string&name);
     120             :   void start();
     121             : /// Stop timer named "name"
     122             :   void stop(const std::string&name);
     123             :   void stop();
     124             : /// Pause timer named "name"
     125             :   void pause(const std::string&name);
     126             :   void pause();
     127             : /// Dump all timers on an ostream
     128             :   friend std::ostream& operator<<(std::ostream&,const Stopwatch&);
     129             : };
     130             : 
     131             : inline
     132      288302 : void Stopwatch::start() {
     133      576604 :   start("");
     134      288302 : }
     135             : 
     136             : inline
     137        2655 : void Stopwatch::stop() {
     138        5310 :   stop("");
     139        2655 : }
     140             : 
     141             : inline
     142      285600 : void Stopwatch::pause() {
     143      571200 :   pause("");
     144      285600 : }
     145             : 
     146             : }
     147             : 
     148             : 
     149             : #endif

Generated by: LCOV version 1.13