Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:50

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file corecel/sys/Stopwatch.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <chrono>
0011 
0012 #include "corecel/Types.hh"
0013 
0014 namespace celeritas
0015 {
0016 //---------------------------------------------------------------------------//
0017 /*!
0018  * Simple timer.
0019  *
0020  * The stopwatch starts counting upward at construction and can be reset by
0021  * assigning a new stopwatch instance.
0022  *
0023  * \code
0024     Stopwatch get_elapsed_time;
0025     // ...
0026     double time = get_elapsed_time();
0027     // Reset the stopwatch
0028     get_elapsed_time = {};
0029    \endcode
0030  */
0031 class Stopwatch
0032 {
0033   public:
0034     // Start the count at construction
0035     inline Stopwatch();
0036 
0037     // Get the current elapsed time [s]
0038     inline double operator()() const;
0039 
0040   private:
0041     using Clock = std::chrono::high_resolution_clock;
0042     using TimePoint = Clock::time_point;
0043     using Duration = Clock::duration;
0044 
0045     TimePoint start_;
0046 };
0047 
0048 //---------------------------------------------------------------------------//
0049 // INLINE DEFINITIONS
0050 //---------------------------------------------------------------------------//
0051 /*!
0052  * Start the count at construction.
0053  */
0054 Stopwatch::Stopwatch() : start_(Clock::now()) {}
0055 
0056 //---------------------------------------------------------------------------//
0057 /*!
0058  * Get the current elapsed time in seconds.
0059  */
0060 double Stopwatch::operator()() const
0061 {
0062     using DurationSec = std::chrono::duration<double>;
0063 
0064     auto duration = Clock::now() - start_;
0065     return std::chrono::duration_cast<DurationSec>(duration).count();
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 }  // namespace celeritas