Advertisement
Jarquafelmu

DurationTracker.cpp

Nov 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include "DurationTracker.h"
  2.  
  3. #include <iomanip>
  4. #include <chrono>
  5. #include <string>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9. using namespace chrono;
  10.  
  11. // Function Name:   DurationTracker: Default Constructor
  12. // Purpose:         Creates a new instance of DurationTracker.
  13. // Parameters:      none
  14. // Returns:         none
  15. // Pre-conditions:  none
  16. // Post-conditions: none
  17. DurationTracker::DurationTracker ()
  18. {
  19.     _startTime = high_resolution_clock::now ();
  20.     _endTime = high_resolution_clock::now ();
  21.     _timeElapsed = 0;
  22. }
  23.  
  24. // Function Name:   DurationTracker: Deconstructor
  25. // Purpose:         Deconstructs the instances of this class when they leave scope
  26. // Parameters:      none
  27. // Returns:         none
  28. // Pre-conditions:  none
  29. // Post-conditions: none
  30. DurationTracker::~DurationTracker () = default;
  31.  
  32. // Function Name:   start
  33. // Purpose:         Records the current time for comparison later.
  34. // Parameters:      none
  35. // Returns:         none
  36. // Pre-conditions:  none
  37. // Post-conditions: none
  38. void DurationTracker::start ()
  39. {
  40.     _startTime = high_resolution_clock::now ();
  41. }
  42.  
  43. // Function Name:   end
  44. // Purpose:         Records the ending time and determines the duration between the start and end.
  45. // Parameters:      none
  46. // Returns:         The duration between the start and end in microseconds.
  47. // Pre-conditions:  none
  48. // Post-conditions: none
  49. void DurationTracker::stop ()
  50. {
  51.     _endTime = high_resolution_clock::now ();
  52.  
  53.     process ();
  54. }
  55.  
  56. void DurationTracker::process ()
  57. {
  58.     duration<double, micro> time = _endTime - _startTime;
  59.     _timeElapsed = time.count ();
  60. }
  61.  
  62. // Function Name:   results
  63. // Purpose:         Provides the formatted duration.
  64. // Parameters:      none
  65. // Returns:         The duration the duration in microseconds or milliseconds depending on the size.
  66. // Pre-conditions:  none
  67. // Post-conditions: none
  68. string DurationTracker::results () const
  69. {
  70.     stringstream ss;
  71.  
  72.     ss << _timeElapsed << " " << "microseconds";
  73.  
  74.     return ss.str ();
  75. }
  76.  
  77. // Function Name:   getTimeElapsed
  78. // Purpose:         Gets the elapsed time for the caller.
  79. // Parameters:      none
  80. // Returns:         The elapsed time as a long.
  81. // Pre-conditions:  none
  82. // Post-conditions: none
  83. double DurationTracker::getTimeElapsed () const
  84. {
  85.     return _timeElapsed;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement