CheckTime.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "../global.h"
  3. #ifdef _WIN32
  4. #include <ctime>
  5. typedef time_t TTime;
  6. #define GET_TIME(var) (var = clock())
  7. #else
  8. #include <sys/time.h> // for gettimeofday()
  9. typedef timeval TTime;
  10. #define GET_TIME(var) (gettimeofday(&var, NULL))
  11. #endif
  12. struct CheckTime
  13. {
  14. TTime start;
  15. std::string msg;
  16. CheckTime(const std::string & Msg = "") : msg(Msg)
  17. {
  18. GET_TIME(start);
  19. }
  20. int timeDiff(const TTime &t1, const TTime &t2)
  21. {
  22. int ret = 0;
  23. #ifdef _WIN32
  24. ret = (float)(t2 - t1) / (CLOCKS_PER_SEC / 1000);
  25. #else
  26. ret += (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
  27. ret += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
  28. #endif
  29. //TODO abs?
  30. return ret;
  31. }
  32. int timeSinceStart()
  33. {
  34. TTime now;
  35. GET_TIME(now);
  36. return timeDiff(start, now);
  37. }
  38. ~CheckTime()
  39. {
  40. if(msg.size())
  41. {
  42. float liczyloSie = timeSinceStart();
  43. tlog0 << msg << ": " << liczyloSie << "ms" << std::endl;
  44. }
  45. }
  46. };
  47. //all ms
  48. const int PROCESS_INFO_TIME = 5;
  49. const int MAKE_DECIDION_TIME = 150;
  50. const int MEASURE_MARGIN = 3000000;
  51. const int HANGUP_TIME = 250;
  52. const int CONSTRUCT_TIME = 50;
  53. const int STARTUP_TIME = 100;
  54. const int TACTICS_TIME = 1000;
  55. void postInfoCall(int timeUsed);
  56. void postDecisionCall(int timeUsed, const std::string &text = "AI was thinking over an action", int timeLimit = MAKE_DECIDION_TIME);
  57. void mySleep(int ms);
  58. struct Bomb
  59. {
  60. std::string txt;
  61. int armed;
  62. void run(int time)
  63. {
  64. //boost::this_thread::sleep(boost::posix_time::milliseconds(time));
  65. mySleep(time);
  66. if(armed)
  67. {
  68. tlog1 << "BOOOM! The bomb exploded! AI was thinking for too long!\n";
  69. if(txt.size())
  70. tlog1 << "Bomb description: " << txt << std::endl;;
  71. exit(1);
  72. }
  73. delete this;
  74. }
  75. Bomb(int timer, const std::string &TXT = "") : txt(TXT)
  76. {
  77. boost::thread t(&Bomb::run, this, timer);
  78. t.detach();
  79. }
  80. void disarm()
  81. {
  82. armed = 0;
  83. }
  84. };