平时写程序,总想知道自己的程序跑得有多快,虽然程序用的时间的大小不仅和算法的复杂度有关,也和其他方面有关,但是,总是忍不住想去看看运行的时间。
而且,在运行机器学习和图像处理的程序的时候,也是需要对比不同算法的复杂度的,直接在算法层面上进行复杂度分析比较困难,因此,可以简单地采用不同算法的运行时间来确定。
上面啰嗦了那么多写这个类的原因,下面就把我写的这个类,请大家赐教。
下面是Timer类的定义
/** * @file timer.h * @brief The definition of the timer class. */ #ifndef TIMER_H #define TIMER_H #include <time.h> namespace Utility { /** * @class Timer * @brief The Timer class which be used to timing. */ class Timer { public: Timer(); bool Start(); bool Finish(); long GetDurationInClocks(); double GetDuration(); private: clock_t Starting; clock_t Finished; clock_t DurationInClocks; double Duration; bool IsStarted; bool IsEnded; }; } #endif // TIMER_H
下面是Timer类的实现
/** * @file timer.cpp * @brief The implement of the timer class. * */ #include "timer.h" #include <assert.h> /** * @brief The defualt constructor * * @author sheng * @version 0.1.0 * @history author version date description \n * sheng 0.1.0 2014/03/08 build the function * */ Utility::Timer::Timer(): Starting(0),Finished(-1), DurationInClocks(-1), Duration(-1.0), IsStarted(false), IsEnded(false) { } /** * @brief Start to timing * @return true if the timer is never started befor * false otherwise * * @author sheng * @version 0.1.0 * @history author version date description \n * sheng 0.1.0 2014/03/08 build the function * */ bool Utility::Timer::Start() { // if the timer is not started befor, starting the timer. assert(!IsStarted); if (IsStarted == false) { IsStarted = true; IsEnded = false; Starting = clock(); return true; } return false; } /** * @brief Finishing the timer * @return true if the timer is started and never finished before. * false otherwise * * @author sheng * @version 0.1.0 * @history author version date description \n * sheng 0.1.0 2014/03/08 build the function * */ bool Utility::Timer::Finish() { // if the timer is started and never finished before, return true. assert(IsStarted && (!IsEnded)); if (IsStarted && (!IsEnded)) { IsEnded = true; Finished = clock(); DurationInClocks = Finished - Starting; Duration = (double)DurationInClocks / CLOCKS_PER_SEC; } return false; } /** * @brief Get the duration in clocks. * @return the duration in clocks. * * @author sheng * @version 0.1.0 * @history author version date description \n * sheng 0.1.0 2014/03/08 build the function * */ long Utility::Timer::GetDurationInClocks() { return DurationInClocks; } /** * @brief Get the duration in seconds. * @return the duration in seconds * * @author sheng * @version 0.1.0 * @history author version date description \n * sheng 0.1.0 2014/03/08 build the function * */ double Utility::Timer::GetDuration() { return Duration; }
下面是测试这个类的函数
#include "timer.h" #include <iostream> using namespace std; /** * @brief The unit test for the Timer */ void Timer_Test() { // test in normol way Utility::Timer timer; timer.Start(); for (int i = 0; i < 200; i++) { cout << i << endl; } timer.Finish(); cout << "Duration in timer is " << timer.GetDuration() << endl; // test the finish before start Utility::Timer timer_finish; timer_finish.Finish(); // test the started after finished. timer.Start(); // test the get duration before start Utility::Timer timer_GetDuration; cout << "Duration before started in clocks is" << timer_GetDuration.GetDurationInClocks() << endl; cout << "Duration before strated in second is" << timer_GetDuration.GetDuration() << endl; // test the get duration after start but before finished. timer_GetDuration.Start(); cout << "Duration before started in clocks is" << timer_GetDuration.GetDurationInClocks() << endl; cout << "Duration before strated in second is" << timer_GetDuration.GetDuration() << endl; }