Apollo  v5.5.0
Open source self driving car software
timer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #pragma once
18 
19 #include <string>
20 
21 namespace apollo {
22 namespace perception {
23 namespace lib {
24 
25 class Timer {
26  public:
27  Timer() : start_time_(0), end_time_(0) {}
28 
29  // no-thread safe.
30  void Start();
31 
32  // return the elapsed time,
33  // also output msg and time in glog.
34  // automatically start a new timer.
35  // no-thread safe.
36  uint64_t End(const std::string &msg);
37 
38  Timer(const Timer &) = delete;
39  Timer &operator=(const Timer &) = delete;
40 
41  private:
42  // in ms.
43  uint64_t start_time_;
44  uint64_t end_time_;
45 };
46 
47 class TimerWrapper {
48  public:
49  explicit TimerWrapper(const std::string &msg) : msg_(msg) { timer_.Start(); }
50 
51  ~TimerWrapper() { timer_.End(msg_); }
52 
53  TimerWrapper(const TimerWrapper &) = delete;
54  TimerWrapper &operator=(const TimerWrapper &) = delete;
55 
56  private:
57  Timer timer_;
58  std::string msg_;
59 };
60 
61 } // namespace lib
62 } // namespace perception
63 } // namespace apollo
Definition: blob.h:72
Timer & operator=(const Timer &)=delete
Timer()
Definition: timer.h:27
uint64_t End(const std::string &msg)
Definition: timer.h:25
TimerWrapper(const std::string &msg)
Definition: timer.h:49
~TimerWrapper()
Definition: timer.h:51