timeHandler.h 1.2 KB

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