Created
July 9, 2017 19:16
-
-
Save tklee1975/6b9562cad67c7b59dc277a7a922faf9d to your computer and use it in GitHub Desktop.
TimeMeter - A simple C++ code to measure the running time of the block of codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // TimeMeter.cpp | |
| // GhostLeg | |
| // | |
| // Created by Ken Lee on 7/7/2017. | |
| // | |
| // | |
| #include "TimeMeter.h" | |
| static TimeMeter *sInstance = nullptr; | |
| namespace { | |
| long long getCurrentTime() { | |
| auto timeNow = std::chrono::system_clock::now().time_since_epoch(); | |
| std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow); | |
| long long timeMillis = ms.count(); | |
| return timeMillis; | |
| } | |
| } | |
| TimeMeter *TimeMeter::instance() | |
| { | |
| if(!sInstance) { | |
| sInstance = new TimeMeter(); | |
| } | |
| return sInstance; | |
| } | |
| TimeMeter::TimeMeter() | |
| : mTimeFilter(0) | |
| , mFilter("") | |
| { | |
| } | |
| void TimeMeter::reset() | |
| { | |
| mStartTimeMap.clear(); | |
| mMaxTimeMap.clear(); | |
| mTotalTimeMap.clear(); | |
| } | |
| std::string TimeMeter::summary() | |
| { | |
| std::string result = ""; | |
| result = "Total Time Used:\n"; | |
| for (auto it=mTotalTimeMap.begin(); it!=mTotalTimeMap.end(); ++it) { | |
| result += StringUtils::format("%s: %lld\n", it->first.c_str(), it->second); | |
| } | |
| result += "---\n"; | |
| result += "Max Time Used:\n"; | |
| for (auto it=mMaxTimeMap.begin(); it!=mMaxTimeMap.end(); ++it) { | |
| result += StringUtils::format("%s: %lld\n", it->first.c_str(), it->second); | |
| } | |
| return result; | |
| } | |
| void TimeMeter::start(const std::string &key) | |
| { | |
| mStartTimeMap[key] = getCurrentTime(); | |
| mLastKey = key; | |
| } | |
| void TimeMeter::stop() { | |
| stop(mLastKey); | |
| } | |
| void TimeMeter::stop(const std::string &key) | |
| { | |
| if(mStartTimeMap.find(key) == mStartTimeMap.end()){ | |
| return; | |
| } | |
| long long startTime = mStartTimeMap[key]; | |
| long long timeDiff = getCurrentTime() - startTime; | |
| // Record the total | |
| if(mTotalTimeMap.find(key) == mTotalTimeMap.end()){ | |
| mTotalTimeMap[key] = timeDiff; | |
| } else { | |
| mTotalTimeMap[key] += timeDiff; | |
| } | |
| // Record the max | |
| if(mTotalTimeMap.find(key) == mTotalTimeMap.end()){ | |
| mTotalTimeMap[key] = timeDiff; | |
| } else { | |
| mTotalTimeMap[key] += timeDiff; | |
| } | |
| // Record the max | |
| if(mMaxTimeMap.find(key) == mMaxTimeMap.end()){ | |
| mMaxTimeMap[key] = timeDiff; | |
| } else { | |
| long long oldTime = mMaxTimeMap[key]; | |
| if(timeDiff > oldTime) { | |
| mMaxTimeMap[key] = timeDiff; | |
| } | |
| } | |
| if(mTimeFilter <= 0 || timeDiff >= mTimeFilter) { | |
| log("[%s: %lldms]", key.c_str(), timeDiff); | |
| } | |
| } | |
| void TimeMeter::showSummary() | |
| { | |
| log("====================="); | |
| log("TimeMeter: Summary "); | |
| log("====================="); | |
| log("%s", summary().c_str()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // TimeMeter.hpp | |
| // GhostLeg | |
| // | |
| // Created by Ken Lee on 7/7/2017. | |
| // | |
| // | |
| #ifndef TimeMeter_hpp | |
| #define TimeMeter_hpp | |
| #include <stdio.h> | |
| #include <map> | |
| #include "cocos2d.h" | |
| USING_NS_CC; | |
| class TimeMeter | |
| { | |
| public: | |
| static TimeMeter *instance(); | |
| public: | |
| CC_SYNTHESIZE(std::string, mFilter, Filter); | |
| CC_SYNTHESIZE(int, mTimeFilter, TimeFilter); | |
| public: | |
| TimeMeter(); | |
| void reset(); | |
| void start(const std::string &key); | |
| void stop(); | |
| void stop(const std::string &key); | |
| std::string summary(); | |
| void showSummary(); | |
| private: | |
| std::map<std::string, long long> mStartTimeMap; | |
| std::map<std::string, long long> mTotalTimeMap; | |
| std::map<std::string, long long> mMaxTimeMap; | |
| std::string mLastKey; | |
| }; | |
| #endif /* TimeMeter_hpp */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void TimeMeterTest::testSummary() | |
| { | |
| for(int x=1; x<=3; x++) { | |
| int nLoop = 1000000 * x; | |
| std::string key = StringUtils::format("%dSqrt", nLoop); | |
| TimeMeter::instance()->start(key); | |
| for(int i=0; i<nLoop; i++) { | |
| sqrt(1.0f); | |
| } | |
| TimeMeter::instance()->stop(); | |
| } | |
| log("Summary:\n%s\n", TimeMeter::instance()->summary().c_str()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment