Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DurationTracker.h"
- #include <iomanip>
- #include <chrono>
- #include <string>
- #include <sstream>
- using namespace std;
- using namespace chrono;
- // Function Name: DurationTracker: Default Constructor
- // Purpose: Creates a new instance of DurationTracker.
- // Parameters: none
- // Returns: none
- // Pre-conditions: none
- // Post-conditions: none
- DurationTracker::DurationTracker ()
- {
- _startTime = high_resolution_clock::now ();
- _endTime = high_resolution_clock::now ();
- _timeElapsed = 0;
- }
- // Function Name: DurationTracker: Deconstructor
- // Purpose: Deconstructs the instances of this class when they leave scope
- // Parameters: none
- // Returns: none
- // Pre-conditions: none
- // Post-conditions: none
- DurationTracker::~DurationTracker () = default;
- // Function Name: start
- // Purpose: Records the current time for comparison later.
- // Parameters: none
- // Returns: none
- // Pre-conditions: none
- // Post-conditions: none
- void DurationTracker::start ()
- {
- _startTime = high_resolution_clock::now ();
- }
- // Function Name: end
- // Purpose: Records the ending time and determines the duration between the start and end.
- // Parameters: none
- // Returns: The duration between the start and end in microseconds.
- // Pre-conditions: none
- // Post-conditions: none
- void DurationTracker::stop ()
- {
- _endTime = high_resolution_clock::now ();
- process ();
- }
- void DurationTracker::process ()
- {
- duration<double, micro> time = _endTime - _startTime;
- _timeElapsed = time.count ();
- }
- // Function Name: results
- // Purpose: Provides the formatted duration.
- // Parameters: none
- // Returns: The duration the duration in microseconds or milliseconds depending on the size.
- // Pre-conditions: none
- // Post-conditions: none
- string DurationTracker::results () const
- {
- stringstream ss;
- ss << _timeElapsed << " " << "microseconds";
- return ss.str ();
- }
- // Function Name: getTimeElapsed
- // Purpose: Gets the elapsed time for the caller.
- // Parameters: none
- // Returns: The elapsed time as a long.
- // Pre-conditions: none
- // Post-conditions: none
- double DurationTracker::getTimeElapsed () const
- {
- return _timeElapsed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement