12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- #include "RUPerfTimer.h"
- NS_RU_BEGIN
- std::unordered_map<std::string, PerfTimer*> PerfTimer::_gPerfTimers;
- PerfTimer* PerfTimer::instance(std::string key4PerfTimer) {
- auto got = _gPerfTimers.find(key4PerfTimer);
- if (got == _gPerfTimers.end()) {
- auto perfTimer = new PerfTimer();
- perfTimer->autorelease();
- perfTimer->retain();
- perfTimer->_perfTimerName = key4PerfTimer;
- _gPerfTimers.insert(pair<std::string, PerfTimer*>(key4PerfTimer, perfTimer));
- return perfTimer;
- }
- return got->second;
- }
- void PerfTimer::start() {
- _startTime = std::chrono::steady_clock::now();
- }
- void PerfTimer::stop() {
- _endTime = std::chrono::steady_clock::now();
- _duration += _endTime - _startTime;
- }
- void PerfTimer::clear(){
- _duration = std::chrono::steady_clock::duration::zero();
- }
- void PerfTimer::dump(int indent) {
- std::string indentStr = "";
- for (int i = 0; i < indent; ++i){
- indentStr += " ";
- }
- cocos2d::log("【性能分析】%s%s 耗时:%lld 毫秒",indentStr.c_str(), _perfTimerName.c_str(), std::chrono::duration_cast<std::chrono::milliseconds>(_duration).count());
- }
- NS_RU_END
|