CStopWatch.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #ifdef __FreeBSD__
  3. #include <sys/types.h>
  4. #include <sys/time.h>
  5. #include <sys/resource.h>
  6. #define TO_MS_DIVISOR (1000)
  7. #else
  8. #include <ctime>
  9. #define TO_MS_DIVISOR (CLOCKS_PER_SEC / 1000)
  10. #endif
  11. /*
  12. * timeHandler.h, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  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. };