Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/corecel/sys/Stopwatch.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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