RUPerfTimer.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "RUPerfTimer.h"
  2. NS_RU_BEGIN
  3. std::unordered_map<std::string, PerfTimer*> PerfTimer::_gPerfTimers;
  4. PerfTimer* PerfTimer::instance(std::string key4PerfTimer) {
  5. auto got = _gPerfTimers.find(key4PerfTimer);
  6. if (got == _gPerfTimers.end()) {
  7. auto perfTimer = new PerfTimer();
  8. perfTimer->autorelease();
  9. perfTimer->retain();
  10. perfTimer->_perfTimerName = key4PerfTimer;
  11. _gPerfTimers.insert(pair<std::string, PerfTimer*>(key4PerfTimer, perfTimer));
  12. return perfTimer;
  13. }
  14. return got->second;
  15. }
  16. void PerfTimer::start() {
  17. _startTime = std::chrono::steady_clock::now();
  18. }
  19. void PerfTimer::stop() {
  20. _endTime = std::chrono::steady_clock::now();
  21. _duration += _endTime - _startTime;
  22. }
  23. void PerfTimer::clear(){
  24. _duration = std::chrono::steady_clock::duration::zero();
  25. }
  26. void PerfTimer::dump(int indent) {
  27. std::string indentStr = "";
  28. for (int i = 0; i < indent; ++i){
  29. indentStr += " ";
  30. }
  31. cocos2d::log("【性能分析】%s%s 耗时:%lld 毫秒",indentStr.c_str(), _perfTimerName.c_str(), std::chrono::duration_cast<std::chrono::milliseconds>(_duration).count());
  32. }
  33. NS_RU_END