2
0

CStopWatch.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * CStopWatch.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #ifdef __FreeBSD__
  12. #include <sys/types.h>
  13. #include <sys/time.h>
  14. #include <sys/resource.h>
  15. #define TO_MS_DIVISOR (1000)
  16. #else
  17. #include <ctime>
  18. #define TO_MS_DIVISOR (CLOCKS_PER_SEC / 1000)
  19. #endif
  20. class CStopWatch
  21. {
  22. si64 start, last, mem;
  23. public:
  24. CStopWatch()
  25. : start(clock())
  26. {
  27. last=clock();
  28. mem=0;
  29. }
  30. si64 getDiff() //get diff in milliseconds
  31. {
  32. si64 ret = clock() - last;
  33. last = clock();
  34. return ret / TO_MS_DIVISOR;
  35. }
  36. void update()
  37. {
  38. last=clock();
  39. }
  40. void remember()
  41. {
  42. mem=clock();
  43. }
  44. si64 memDif()
  45. {
  46. return (clock()-mem) / TO_MS_DIVISOR;
  47. }
  48. private:
  49. si64 clock()
  50. {
  51. #ifdef __FreeBSD__
  52. struct rusage usage;
  53. getrusage(RUSAGE_SELF, &usage);
  54. return static_cast<si64>(usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) * 1000000 + usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
  55. #else
  56. return std::clock();
  57. #endif
  58. }
  59. };