Timer 类 - 测量程序的运行时间
以毫秒级的精度测量应用程序或一组进程的运行时间。
引言
这个计时器类用于确定一组进程的运行时间,精度达到毫秒级。 如果你想基准测试电脑,或者想知道更改应用程序中的某些代码是否会影响其处理速度,这会很有用。
背景
我创建这个类是因为我找不到另一个类或一组函数可以测量我的应用程序运行所需的时间。 在创建计时器的初始对象类之后,我又对其进行了扩展,创建了现在我正在描述的类。
使用代码
接口包含一个对象:timer
。 为了使用这个类,你需要使用默认构造函数或布尔构造函数创建一个对象实例。
要创建一个计时器实例,请使用以下语句之一
timer newTimerInstance(); //Create a timer instance
timer newTimerInstance(true); //Use this constructor to start the timer as
//soon as the object is created
你可以使用以下语句启动、停止和重新启动计时器
newTimerInstance.start(); //Start the timer
newTimerInstance.stop(); //Stop the timer
newTimerInstance.start(); //Start the timer again after having been
//stopped the timer previously, continuing
//accumulating the time
要重置累积时间
newTimerInstance.reset();
//This sets the timer back to 0 so it can be restarted again using start()
你可以直接使用“<<
”输出计时器的运行长度,或者将对象存储为 string
、int
或 double
std::cout << newTimerInstance; //Output the running length of the timer
std::string newString = newTimerInstance; //Store the running length of the
//timer as a string
int newInt = newTimerInstance; //Store the number of seconds which the timer
//has been running for in an int
double newDouble = newTimerInstance; //Store the number of seconds which the
//timer has been running for in a double
有关如何使用该类的更多信息,请参阅软件包中包含的 example.cpp。
历史
- 计时器类 1.1 - API已修改,以更接近传统计时器的表示方式。
Restart
方法已被Reset
替换。 现在使用 MS 对象命名约定。 - 计时器类 1.2 - API重新设计,希望使使用更简单。 添加了
Pause
和Resume
方法,并修改了Start
和Stop
方法的功能。 这使得计时器概念更直观。
更多信息,请访问:www.summatix.com。