如何使用
- #include "sperformance.h"
- #include <iostream>
- #include <boost/thread.hpp>
- int main(int argc, char** argv)
- {
- kagula::PerformanceTest pt;
- {
- pt.start();
- boost::this_thread::sleep(boost::posix_time::milliseconds(0));
- pt.stop();
- std::cout << "First time:" << pt.toString() << std::endl << std::endl;
- }
- {
- pt.start();
- boost::this_thread::sleep(boost::posix_time::milliseconds(100));
- pt.stop();
- std::cout << "Second time:" << pt.toString() << std::endl << std::endl;
- }
- {
- pt.start();
- boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
- pt.stop();
- std::cout << "Third time:" << pt.toString() << std::endl << std::endl;
- }
- std::cin.get();
- return 0;
- }
依赖的头文件
- #ifndef _SPERFORMANCE_H_
- #define _SPERFORMANCE_H_
- #include <string>
- #include <list>
- #include <boost/thread/mutex.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- namespace kagula
- {
- /*
- * Title:
- * Description:
- * 以毫秒(Millisecond)为单位测试程序代码段的运行时间。
- * Dependencies: boost 1.61
- * Test Environment: VS2013 Update5
- * Remark:
- * [1]计量单位ms
- * [2]优点:[a]使用简单[2]只依赖boost,可以跨平台.
- * */
- struct PerformanceTest
- {
- PerformanceTest() :m_lastFPS(.0f),
- m_minElapsedTime(1000.0f), m_maxElapsedTime(.0f), m_lastElapsedTime(.0f){}
- std::string toString();
- void start();
- void stop();
- float getMinFPS();
- float getMaxFPS();
- float getLastFPS();
- float getAvgFPS();
- float getElapsedTime();
- float getAvgTime();
- private:
- float m_minFPS;
- float m_maxFPS;
- float m_lastFPS;
- float m_avgFPS;
- float m_minElapsedTime;////milli second unit
- float m_maxElapsedTime;
- float m_avgElapsedTime;
- float m_lastElapsedTime;
- std::list<float> m_listHistoryElapsedTime;
- void setInMilliSecond(unsigned long long milliSecond);
- boost::mutex m_mutexRW;
- boost::posix_time::ptime m_timeStart;
- };
- //std::string GetCurrentTime();
- }
- #endif
依赖的实现文件
- #include "sperformance.h"
- #include <boost/thread/locks.hpp>
- #include <iomanip>
- namespace kagula
- {
- void PerformanceTest::start()
- {
- m_timeStart = boost::posix_time::microsec_clock::universal_time();
- }
- void PerformanceTest::stop()
- {
- boost::posix_time::ptime timeStop = boost::posix_time::microsec_clock::universal_time();
- boost::posix_time::millisec_posix_time_system_config::time_duration_type timeElapsed;
- timeElapsed = timeStop - m_timeStart;
- unsigned long long llElapsedTime = timeElapsed.total_milliseconds();
- setInMilliSecond(llElapsedTime);
- }
- void PerformanceTest::setInMilliSecond(unsigned long long milliSecond)
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- //Prevent division zero error.
- if (milliSecond <= 0)
- milliSecond = 1;
- m_lastElapsedTime = milliSecond;
- //
- if (m_minElapsedTime > milliSecond)
- m_minElapsedTime = milliSecond;
- m_maxFPS = 1000 / m_minElapsedTime;
- //
- if (m_maxElapsedTime < milliSecond)
- m_maxElapsedTime = milliSecond;
- m_minFPS = 1000 / m_maxElapsedTime;
- //
- m_lastFPS = 1000 / (float)milliSecond;
- //
- m_listHistoryElapsedTime.push_back(m_lastElapsedTime);
- std::list<float>::iterator iter = m_listHistoryElapsedTime.begin();
- float fTemp = .0f;
- while (iter != m_listHistoryElapsedTime.end())
- {
- fTemp += *iter;
- iter++;
- }
- m_avgElapsedTime = fTemp / m_listHistoryElapsedTime.size();
- m_avgFPS = 1000 / m_avgElapsedTime;
- if (m_listHistoryElapsedTime.size() > 1024)
- {
- m_listHistoryElapsedTime.pop_front();
- //http://www.cplusplus.com/reference/list/list/pop_front/
- }
- }//end function
- std::string PerformanceTest::toString()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- std::stringstream ss;
- ss << "Last elapsed time is " << std::setw(5) << std::setprecision(2) << std::fixed
- << m_lastElapsedTime
- << "ms, last fps is " << m_lastFPS
- << " ,minimum elapsed time is " << m_minElapsedTime
- << "ms, maximum elapsed time is " << m_maxElapsedTime
- << "ms, avelage elapsed time is " << m_avgElapsedTime
- << "ms, average fps is " << m_avgFPS;
- return ss.str();
- }
- float PerformanceTest::getElapsedTime()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- return m_lastElapsedTime;
- }
- float PerformanceTest::getAvgTime()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- return m_avgElapsedTime;
- }
- float PerformanceTest::getAvgFPS()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- return m_avgFPS;
- }
- float PerformanceTest::getMinFPS()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- return m_minFPS;
- }
- float PerformanceTest::getMaxFPS()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- return m_maxFPS;
- }
- float PerformanceTest::getLastFPS()
- {
- boost::lock_guard<boost::mutex> lock(m_mutexRW);
- return m_lastFPS;
- }
- boost::mutex g_mutexGetCurrentTime;
- std::string GetCurrentTime()
- {
- boost::lock_guard<boost::mutex> lock(g_mutexGetCurrentTime);
- //下面这段代码可能会导致boost::log多线程冲突问题
- boost::posix_time::time_facet* p_tfacet = new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S%F");
- std::stringstream ss;
- ss.imbue(std::locale(ss.getloc(), p_tfacet));
- ss << "[" << boost::posix_time::microsec_clock::local_time() << "]: ";
- return ss.str();
- }
- }
http://blog.csdn.net/lee353086/article/details/53741514