![]() |
|
|||
File indexing completed on 2025-04-04 07:57:50
0001 // This file is part of the ACTS project. 0002 // 0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project 0004 // 0005 // This Source Code Form is subject to the terms of the Mozilla Public 0006 // License, v. 2.0. If a copy of the MPL was not distributed with this 0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/. 0008 0009 #pragma once 0010 0011 #include "Acts/Utilities/Logger.hpp" 0012 0013 #include <chrono> 0014 #include <iostream> 0015 0016 namespace Acts { 0017 0018 /// @brief A RAII timer class for measuring execution time of code blocks 0019 /// 0020 /// ScopedTimer provides automatic timing of code blocks using RAII principles. 0021 /// It starts timing when constructed and automatically logs the duration when 0022 /// destroyed. This makes it ideal for measuring execution time of functions 0023 /// or code blocks without manual start/stop calls. 0024 /// 0025 /// Example usage: 0026 /// @code 0027 /// { 0028 /// ScopedTimer timer("myFunction"); 0029 /// // ... code to measure ... 0030 /// } // Timer automatically logs duration when block ends 0031 /// @endcode 0032 /// 0033 class ScopedTimer { 0034 public: 0035 using clock_type = std::chrono::high_resolution_clock; 0036 0037 /// @brief Construct a new Scoped Timer 0038 /// 0039 /// @param name Identifier for the timed block 0040 /// @param logger Logger instance to use for output 0041 /// @param lvl Logging level for the timing output 0042 explicit ScopedTimer(const std::string& name, const Logger& logger, 0043 Logging::Level lvl = Logging::Level::INFO); 0044 0045 /// @brief Destructor that logs the execution time 0046 /// 0047 /// Automatically calculates and logs the duration between construction 0048 /// and destruction using the specified logger and level. 0049 ~ScopedTimer(); 0050 0051 ScopedTimer(const ScopedTimer&) = delete; 0052 ScopedTimer& operator=(const ScopedTimer&) = delete; 0053 ScopedTimer(ScopedTimer&&) = delete; 0054 ScopedTimer& operator=(ScopedTimer&&) = delete; 0055 0056 private: 0057 std::string m_name; ///< Identifier for the timed block 0058 Logging::Level m_lvl; ///< Logging level for output 0059 const Logger* m_logger; ///< Logger instance for output 0060 clock_type::time_point m_start; ///< Start time of the timer 0061 }; 0062 0063 /// @brief A timer class that measures and averages execution times of multiple samples 0064 /// 0065 /// This class provides functionality to measure execution times of code blocks 0066 /// and calculate statistics (mean, standard deviation) across multiple samples. 0067 /// It uses RAII through the Sample class to automatically record timing 0068 /// information. 0069 class AveragingScopedTimer { 0070 public: 0071 using clock_type = std::chrono::high_resolution_clock; 0072 0073 /// @brief RAII wrapper class for measuring individual timing samples 0074 /// 0075 /// When constructed, starts a timer. When destroyed, automatically records 0076 /// the duration to the parent AveragingScopedTimer. 0077 class Sample { 0078 public: 0079 /// @brief Construct a new sample and start timing 0080 explicit Sample(AveragingScopedTimer& parent); 0081 /// @brief Record the duration when destroyed 0082 ~Sample(); 0083 Sample(const Sample&) = delete; 0084 Sample& operator=(const Sample&) = delete; 0085 /// @brief Move constructor that transfers ownership of timing to new sample 0086 Sample(Sample&& /*other*/); 0087 Sample& operator=(Sample&&) = delete; 0088 0089 private: 0090 AveragingScopedTimer* m_parent; 0091 clock_type::time_point m_start; 0092 }; 0093 0094 /// @brief Construct a new AveragingScopedTimer 0095 /// 0096 /// @param name Name of the timer for logging 0097 /// @param logger Logger instance to use for output 0098 /// @param lvl Logging level for timing output 0099 explicit AveragingScopedTimer(const std::string& name, const Logger& logger, 0100 Logging::Level lvl = Logging::Level::INFO); 0101 0102 /// @brief Destroy the AveragingScopedTimer and log statistics 0103 /// 0104 /// Outputs total duration and per-sample statistics (mean ± stddev) if 0105 /// logging is enabled at the configured level. 0106 ~AveragingScopedTimer(); 0107 AveragingScopedTimer(const AveragingScopedTimer&) = delete; 0108 AveragingScopedTimer& operator=(const AveragingScopedTimer&) = delete; 0109 AveragingScopedTimer(AveragingScopedTimer&&) = delete; 0110 AveragingScopedTimer& operator=(AveragingScopedTimer&&) = delete; 0111 0112 /// @brief Create a new timing sample 0113 /// 0114 /// @return Sample RAII wrapper for measuring a single timing sample 0115 Sample sample(); 0116 0117 friend class Sample; 0118 0119 private: 0120 /// @brief Add a timing sample to the statistics 0121 /// 0122 /// @param duration Duration of the sample in milliseconds 0123 void addSample(std::chrono::milliseconds duration); 0124 0125 double m_sumDuration = 0; ///< Sum of all sample durations 0126 double m_sumDurationSquared = 0127 0; ///< Sum of squared durations for stddev calculation 0128 std::size_t m_nSamples = 0; ///< Number of samples recorded 0129 0130 std::string m_name; ///< Name of the timer for logging 0131 Logging::Level m_lvl; ///< Logging level for output 0132 const Logger* m_logger; ///< Logger instance for output 0133 }; 0134 } // namespace Acts
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |