stopwatch.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include "stopwatch.hpp"
  4. using namespace std;
  5. StopWatch::StopWatch(): mRounds(0)
  6. {
  7. }
  8. void StopWatch::start()
  9. {
  10. mStartTime.getCurrentTime();
  11. }
  12. void StopWatch::stop()
  13. {
  14. mStopTime.getCurrentTime();
  15. mStopTime.subtract(mStartTime);
  16. mStopTime.print();
  17. }
  18. void StopWatch::stopRound()
  19. {
  20. mStopTime.getCurrentTime();
  21. Time TotalDiff = mStopTime - mStartTime;
  22. Time RoundDiff = mStopTime - mLastStopTime;
  23. mLastStopTime = mStopTime;
  24. cout << "Round " << mRounds++ <<":\n";
  25. RoundDiff.print();
  26. cout << "Total:" << "\n";
  27. TotalDiff.print();
  28. }
  29. Time& Time::subtract(const Time& Subtrahent)
  30. {
  31. mTmsTime.tms_utime -= Subtrahent.mTmsTime.tms_utime;
  32. mTmsTime.tms_stime -= Subtrahent.mTmsTime.tms_stime;
  33. mRealTime -= Subtrahent.mRealTime;
  34. return *this;
  35. }
  36. Time Time::operator-(const Time& Subtrahent) const
  37. {
  38. Time Result(*this);
  39. return Result.subtract(Subtrahent);
  40. }
  41. void Time::print()
  42. {
  43. double UserTime = static_cast<double>(mTmsTime.tms_utime) / mTicksPerSec;
  44. double SystemTime = static_cast<double>(mTmsTime.tms_stime) / mTicksPerSec;
  45. double RealTime = static_cast<double>(mRealTime) / mTicksPerSec;
  46. cout << "User time = " << UserTime << "s\n";
  47. cout << "System time = " << SystemTime << "s\n";
  48. cout << "Real time = " << RealTime << "s\n";
  49. }