NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
timers.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef TRAJ_OPT_PRO_TIMERS_H_
20 #define TRAJ_OPT_PRO_TIMERS_H_
21 
22 #include <chrono>
23 #include <iostream>
24 #include <string>
25 // #include <ros/ros.h>
26 
27 namespace traj_opt {
28 // scoped timer
29 class ScopedTimer {
30  public:
31  explicit ScopedTimer(const std::string &str) {
32  msg_ = str;
33  start_ = std::chrono::system_clock::now();
34  }
36  std::chrono::time_point<std::chrono::system_clock> end;
37  end = std::chrono::system_clock::now();
38  std::chrono::duration<double> dt = end - start_;
39  std::cout << msg_ << " took " << dt.count() << " seconds!" << std::endl;
40  }
41 
42  private:
43  std::string msg_;
44  std::chrono::time_point<std::chrono::system_clock> start_;
45 };
46 // tic toc timer
47 class Timer {
48  public:
49  Timer() { tic(); }
50  void tic() { start_ = std::chrono::system_clock::now(); }
51  double toc() {
52  std::chrono::time_point<std::chrono::system_clock> end;
53  end = std::chrono::system_clock::now();
54  std::chrono::duration<double> dt = end - start_;
55  return dt.count();
56  }
57 
58  private:
59  std::chrono::time_point<std::chrono::system_clock> start_;
60 };
61 } // namespace traj_opt
62 #endif // TRAJ_OPT_PRO_TIMERS_H_
traj_opt::Timer
Definition: timers.h:47
traj_opt
Definition: msg_traj.h:27
traj_opt::Timer::tic
void tic()
Definition: timers.h:50
traj_opt::ScopedTimer
Definition: timers.h:29
traj_opt::ScopedTimer::ScopedTimer
ScopedTimer(const std::string &str)
Definition: timers.h:31
traj_opt::ScopedTimer::~ScopedTimer
~ScopedTimer()
Definition: timers.h:35
traj_opt::Timer::Timer
Timer()
Definition: timers.h:49
traj_opt::Timer::toc
double toc()
Definition: timers.h:51