| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifndef GEOGRAM_BASIC_STOPWATCH | ||
| 41 | #define GEOGRAM_BASIC_STOPWATCH | ||
| 42 | |||
| 43 | #include <geogram/basic/common.h> | ||
| 44 | #include <geogram/basic/numeric.h> | ||
| 45 | #include <geogram/basic/logger.h> | ||
| 46 | |||
| 47 | #include <chrono> | ||
| 48 | |||
| 49 | /** | ||
| 50 | * \file geogram/basic/stopwatch.h | ||
| 51 | * \brief Classes for measuring time | ||
| 52 | */ | ||
| 53 | |||
| 54 | namespace GEO { | ||
| 55 | |||
| 56 | /** | ||
| 57 | * \brief Scope restricted stopwatch | ||
| 58 | * \details Stopwatch prints the elapsed time | ||
| 59 | * since its construction when it goes out of scope. | ||
| 60 | * It uses SystemStopwatch to measure time. | ||
| 61 | * | ||
| 62 | * \code | ||
| 63 | * { | ||
| 64 | * Stopwatch W("compute my stuff") ; | ||
| 65 | * ... do something ... | ||
| 66 | * } // <- W prints the elapsed time here. | ||
| 67 | * \endcode | ||
| 68 | */ | ||
| 69 | class GEOGRAM_API Stopwatch { | ||
| 70 | public: | ||
| 71 | /** | ||
| 72 | * \brief Stopwatch constructor | ||
| 73 | * \param[in] task_name name of the job to measure. This name is | ||
| 74 | * used as a Logger feature when displaying the elapsed time. | ||
| 75 | * \param[in] verbose if true, then elapsed time is displayed | ||
| 76 | * when this Stopwatch is destroyed, else nothing is displayed. | ||
| 77 | */ | ||
| 78 | Stopwatch(const std::string& task_name, bool verbose=true); | ||
| 79 | |||
| 80 | |||
| 81 | /** | ||
| 82 | * \brief Stopwatch constructor | ||
| 83 | * \details Constructs a silent (verbose=false) Stopwatch | ||
| 84 | */ | ||
| 85 | Stopwatch(); | ||
| 86 | |||
| 87 | /** | ||
| 88 | * \brief Get the user elapsed time | ||
| 89 | * \details Returns the user time elapsed since the StopWatch | ||
| 90 | * construction (in seconds) | ||
| 91 | */ | ||
| 92 | double elapsed_time() const; | ||
| 93 | |||
| 94 | /** | ||
| 95 | * \brief Stopwatch destructor | ||
| 96 | * \details This prints the time elapsed since the Stopwatch | ||
| 97 | * construction to the Logger if verbose was set in the constructor. | ||
| 98 | */ | ||
| 99 | ~Stopwatch(); | ||
| 100 | |||
| 101 | |||
| 102 | /** | ||
| 103 | * \brief Prints elapsed time to the Logger since the Stopwatch | ||
| 104 | * construction. | ||
| 105 | * \details Always print, even if verbose was not set in the constructor. | ||
| 106 | */ | ||
| 107 | void print_elapsed_time(); | ||
| 108 | |||
| 109 | /** | ||
| 110 | * \details Gets the current time since epoch (in seconds). | ||
| 111 | */ | ||
| 112 | static double now(); | ||
| 113 | |||
| 114 | /** | ||
| 115 | * \details Gets the total elapsed time since process start (in seconds). | ||
| 116 | */ | ||
| 117 | ✗ | static double process_elapsed_time() { | |
| 118 | ✗ | return now() - process_start_time_; | |
| 119 | } | ||
| 120 | |||
| 121 | static void initialize(); | ||
| 122 | static void show_stats(); | ||
| 123 | |||
| 124 | private: | ||
| 125 | static double process_start_time_; | ||
| 126 | static bool global_stats_; | ||
| 127 | std::chrono::time_point<std::chrono::system_clock> start_; | ||
| 128 | std::string task_name_; | ||
| 129 | bool verbose_; | ||
| 130 | }; | ||
| 131 | } | ||
| 132 | |||
| 133 | #endif | ||
| 134 |