Gudhi  1.1.0
 All Classes Functions Typedefs Friends Groups Pages
Clock.h
1  /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): David Salinas
6  *
7  * Copyright (C) 2014 INRIA Sophia Antipolis-Mediterranee (France)
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU 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  * This program 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef GUDHI_CLOCK_H_
24 #define GUDHI_CLOCK_H_
25 
26 
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 
29 class Clock{
30 
31 public:
32  Clock():end_called(false){
33  startTime = boost::posix_time::microsec_clock::local_time( );
34  }
35 
36  Clock(const std::string& msg_){
37  end_called = false;
38  begin();
39  msg = msg_;
40  }
41 
42 
43  void begin() const{
44  end_called = false;
45  startTime = boost::posix_time::microsec_clock::local_time( );
46  }
47 
48  void end() const{
49  end_called = true;
50  endTime = boost::posix_time::microsec_clock::local_time( );
51  }
52 
53  void print() const{
54  std::cout << *this << std::endl;
55  }
56 
57  friend std::ostream& operator<< (std::ostream& stream,const Clock& clock){
58  if(!clock.end_called)
59  clock.end();
60 
61  if(!clock.end_called) stream << "end not called";
62  else{
63  stream << clock.msg <<":"<<clock.num_seconds() <<"s";
64  }
65  return stream;
66 
67  }
68 
69  double num_seconds() const{
70  if(!end_called) return -1;
71  return (endTime-startTime).total_milliseconds()/1000.;
72  }
73 
74 private:
75  mutable boost::posix_time::ptime startTime, endTime;
76  mutable bool end_called;
77  std::string msg;
78 
79 };
80 
81 
82 #endif /* GUDHI_CLOCK_H_ */