GCC Code Coverage Report


Directory: ./
File: include/real_time_tools/checkpoint_timer.hpp
Date: 2022-06-29 13:58:11
Exec Total Coverage
Lines: 0 1 0.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /**
2 * @file
3 * @license BSD 3-clause
4 * @copyright Copyright (c) 2020, New York University and Max Planck
5 * Gesellschaft
6 *
7 * @brief Implementation of the CheckpointTimer class.
8 */
9
10 #pragma once
11
12 #include <array>
13 #include <iostream>
14 #include <string>
15
16 #include "timer.hpp"
17
18 namespace real_time_tools
19 {
20 /**
21 * @brief Timer to measure code execution time with "checkpoints"
22 *
23 * This timer is meant to be used for measuring execution time of a loop. It
24 * measures time between calls of the `start` method, so by calling this at the
25 * beginning of the loop, you get the execution time of the full iteration.
26 * Further, you can define "checkpoints" within the loop to measure time of
27 * separate steps in the loop. Call the `checkpoint` method after the code that
28 * is associated with it. For each checkpoint, the time elapsed since the last
29 * checkpoint is measured (`start` counts as a checkpoint in this regard).
30 *
31 * Example:
32 * @snippet demo_checkpoint_timer.cpp Usage of CheckpointTimer
33 *
34 * @tparam NUM_CHECKPOINTS Number of checkpoints.
35 * @tparam ENABLED Set to false, to disable timer. Method calls will have no
36 * effect (and should hopefully be optimized away by the compiler).
37 */
38 template <size_t NUM_CHECKPOINTS, bool ENABLED = true>
39 class CheckpointTimer
40 {
41 public:
42 CheckpointTimer();
43
44 //! @brief Start timer iteration.
45 void start();
46
47 /**
48 * @brief Set checkpoint for time measurement.
49 *
50 * Measures time from the last call of start() or checkpoint() until this
51 * call. The given name is used when printing the results.
52 *
53 * @param checkpoint_name Name of the checkpoint (used for printing results)
54 */
55 void checkpoint(const std::string &checkpoint_name);
56
57 //! @brief Print results of time measurements.
58 void print_statistics() const;
59
60 private:
61 //! @brief Timers used for the different checkpoints. Index 0 is used for
62 //! the total duration.
63 std::array<real_time_tools::Timer, NUM_CHECKPOINTS + 1> timers_;
64 //! @brief Names of the checkpoints.
65 std::array<std::string, NUM_CHECKPOINTS + 1> checkpoint_names_;
66 //! @brief Index of the current checkpoint.
67 size_t current_checkpoint_ = 1;
68 };
69
70 #include "checkpoint_timer.hxx"
71 } // namespace real_time_tools
72