Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-21 08:09:06

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 #include "Acts/Utilities/ScopedTimer.hpp"
0010 
0011 #include <chrono>
0012 #include <cmath>
0013 
0014 namespace Acts {
0015 
0016 ScopedTimer::ScopedTimer(const std::string& name, const Logger& logger,
0017                          Logging::Level lvl)
0018     : m_name(name), m_lvl(lvl), m_logger(&logger) {
0019   m_start = clock_type::now();
0020 }
0021 
0022 ScopedTimer::~ScopedTimer() {
0023   auto end = clock_type::now();
0024   auto duration =
0025       std::chrono::duration_cast<std::chrono::milliseconds>(end - m_start);
0026   if (m_logger->doPrint(m_lvl)) {
0027     std::ostringstream oss;
0028     oss << m_name << " took " << duration.count() << " ms";
0029     m_logger->log(m_lvl, oss.str());
0030   }
0031 }
0032 
0033 AveragingScopedTimer::AveragingScopedTimer(const std::string& name,
0034                                            const Logger& logger,
0035                                            Logging::Level lvl)
0036     : m_name(name), m_lvl(lvl), m_logger(&logger) {}
0037 
0038 void AveragingScopedTimer::addSample(std::chrono::milliseconds duration) {
0039   m_sumDuration += duration.count();
0040   m_sumDurationSquared += duration.count() * duration.count();
0041   m_nSamples++;
0042 }
0043 
0044 AveragingScopedTimer::Sample::Sample(AveragingScopedTimer& parent)
0045     : m_parent(&parent), m_start(clock_type::now()) {}
0046 
0047 AveragingScopedTimer::Sample::Sample(Sample&& other)
0048     : m_parent(other.m_parent), m_start(other.m_start) {
0049   // Disable the moved-from sample
0050   other.m_parent = nullptr;
0051 }
0052 
0053 AveragingScopedTimer::Sample AveragingScopedTimer::sample() {
0054   return Sample{*this};
0055 }
0056 
0057 AveragingScopedTimer::Sample::~Sample() {
0058   // parent nullptr means the sample was moved
0059   if (m_parent != nullptr) {
0060     auto end = clock_type::now();
0061     auto duration =
0062         std::chrono::duration_cast<std::chrono::milliseconds>(end - m_start);
0063     m_parent->addSample(duration);
0064   }
0065 }
0066 
0067 AveragingScopedTimer::~AveragingScopedTimer() {
0068   if (m_logger->doPrint(m_lvl)) {
0069     std::ostringstream oss;
0070     if (m_nSamples > 0) {
0071       double mean = m_sumDuration / m_nSamples;
0072       double stddev =
0073           std::sqrt(m_sumDurationSquared / m_nSamples - mean * mean);
0074       oss << m_name << " took " << m_sumDuration << " ms total, " << mean
0075           << " ms +- " << stddev << " ms per sample (#" << m_nSamples << ")";
0076     } else {
0077       oss << m_name << " took " << m_sumDuration << " ms total (no samples)";
0078     }
0079     m_logger->log(m_lvl, oss.str());
0080   }
0081 }
0082 
0083 }  // namespace Acts