IpTimedTask.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2006, 2009 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpTimedTask.hpp 1861 2010-12-21 21:34:47Z andreasw $
  6. //
  7. // Authors: Andreas Waechter IBM 2005-09-19
  8. #ifndef __IPTIMEDTASK_HPP__
  9. #define __IPTIMEDTASK_HPP__
  10. #include "IpUtils.hpp"
  11. namespace Ipopt
  12. {
  13. /** This class is used to collect timing information for a
  14. * particular task. */
  15. class TimedTask
  16. {
  17. public:
  18. /**@name Constructors/Destructors */
  19. //@{
  20. /** Default constructor. */
  21. TimedTask()
  22. :
  23. total_cputime_(0.),
  24. total_systime_(0.),
  25. total_walltime_(0.),
  26. start_called_(false),
  27. end_called_(true)
  28. {}
  29. /** Default destructor */
  30. ~TimedTask()
  31. {}
  32. //@}
  33. /** Method for resetting time to zero. */
  34. void Reset()
  35. {
  36. total_cputime_ = 0.;
  37. total_systime_ = 0.;
  38. total_walltime_ = 0.;
  39. start_called_ = false;
  40. end_called_ = true;
  41. }
  42. /** Method that is called before execution of the task. */
  43. void Start()
  44. {
  45. DBG_ASSERT(end_called_);
  46. DBG_ASSERT(!start_called_);
  47. end_called_ = false;
  48. start_called_ = true;
  49. start_cputime_ = CpuTime();
  50. start_systime_ = SysTime();
  51. start_walltime_ = WallclockTime();
  52. }
  53. /** Method that is called after execution of the task. */
  54. void End()
  55. {
  56. DBG_ASSERT(!end_called_);
  57. DBG_ASSERT(start_called_);
  58. end_called_ = true;
  59. start_called_ = false;
  60. total_cputime_ += CpuTime() - start_cputime_;
  61. total_systime_ += SysTime() - start_systime_;
  62. total_walltime_ += WallclockTime() - start_walltime_;
  63. }
  64. /** Method that is called after execution of the task for which
  65. * timing might have been started. This only updates the timing
  66. * if the timing has indeed been conducted. This is useful to
  67. * stop timing after catching exceptions. */
  68. void EndIfStarted()
  69. {
  70. if (start_called_) {
  71. end_called_ = true;
  72. start_called_ = false;
  73. total_cputime_ += CpuTime() - start_cputime_;
  74. total_systime_ += SysTime() - start_systime_;
  75. total_walltime_ += WallclockTime() - start_walltime_;
  76. }
  77. DBG_ASSERT(end_called_);
  78. }
  79. /** Method returning total CPU time spend for task so far. */
  80. Number TotalCpuTime() const
  81. {
  82. DBG_ASSERT(end_called_);
  83. return total_cputime_;
  84. }
  85. /** Method returning total system time spend for task so far. */
  86. Number TotalSysTime() const
  87. {
  88. DBG_ASSERT(end_called_);
  89. return total_systime_;
  90. }
  91. /** Method returning total wall clock time spend for task so far. */
  92. Number TotalWallclockTime() const
  93. {
  94. DBG_ASSERT(end_called_);
  95. return total_walltime_;
  96. }
  97. private:
  98. /**@name Default Compiler Generated Methods (Hidden to avoid
  99. * implicit creation/calling). These methods are not
  100. * implemented and we do not want the compiler to implement them
  101. * for us, so we declare them private and do not define
  102. * them. This ensures that they will not be implicitly
  103. * created/called. */
  104. //@{
  105. /** Copy Constructor */
  106. TimedTask(const TimedTask&);
  107. /** Overloaded Equals Operator */
  108. void operator=(const TimedTask&);
  109. //@}
  110. /** CPU time at beginning of task. */
  111. Number start_cputime_;
  112. /** Total CPU time for task measured so far. */
  113. Number total_cputime_;
  114. /** System time at beginning of task. */
  115. Number start_systime_;
  116. /** Total system time for task measured so far. */
  117. Number total_systime_;
  118. /** Wall clock time at beginning of task. */
  119. Number start_walltime_;
  120. /** Total wall clock time for task measured so far. */
  121. Number total_walltime_;
  122. /** @name fields for debugging */
  123. //@{
  124. bool start_called_;
  125. bool end_called_;
  126. //@}
  127. };
  128. } // namespace Ipopt
  129. #endif