计算时间,精确到纳秒
来源:互联网 发布:剑三卡卢比捏脸数据 编辑:程序博客网 时间:2024/05/21 09:06
#ifndef __TIMER_H__
#define __TIMER_H__
#include <iostream>
#include <windows.h>
class Timer
{
public:
Timer();
~Timer() {};
void start();
void end();
float getTime() const;
void display() const;
private:
__int64 frequency; // Timer Frequency
float resolution; // Timer Resolution
unsigned long mm_timer_start; // Multimedia Timer Start Value
unsigned long mm_timer_elapsed; // Multimedia Timer Elapsed Time
bool performance_timer; // Using The Performance Timer?
__int64 performance_timer_start; // Performance Timer Start Value
__int64 performance_timer_elapsed; // Performance Timer Elapsed Time
float startTime;
float endTime;
float passTime;
};
Timer::Timer()
{
// Check To See If A Performance Counter Is Available
// If One Is Available The Timer Frequency Will Be Updated
if (!QueryPerformanceFrequency((LARGE_INTEGER *) & frequency)) {
// No Performace Counter Available
performance_timer = FALSE; // Set Performance Timer To FALSE
//mm_timer_start = timeGetTime(); // Use timeGetTime() To Get Current Time
resolution = 1.0f/1000.0f; // Set Our Timer Resolution To .001f
frequency = 1000; // Set Our Timer Frequency To 1000
mm_timer_elapsed = mm_timer_start; // Set The Elapsed Time To The Current Time
} else {
// Performance Counter Is Available, Use It Instead Of The Multimedia Timer
// Get The Current Time And Store It In performance_timer_start
QueryPerformanceCounter((LARGE_INTEGER *) &performance_timer_start);
performance_timer = TRUE; // Set Performance Timer To TRUE
// Calculate The Timer Resolution Using The Timer Frequency
resolution = (float) (((double)1.0f)/((double)frequency));
// Set The Elapsed Time To The Current Time
performance_timer_elapsed = performance_timer_start;
}
}
void Timer::start()
{
__int64 time; // time Will Hold A 64 Bit Integer
if (performance_timer) { // Are We Using The Performance Timer?
QueryPerformanceCounter((LARGE_INTEGER *) &time); // Grab The Current Performance Time
// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
startTime = ( (float) ( time - performance_timer_start) * resolution)*1000.0f;
} else {
// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
//startTime = ( (float) ( timeGetTime() - mm_timer_start) * resolution)*1000.0f;
}
}
void Timer::end()
{
__int64 time; // time Will Hold A 64 Bit Integer
if (performance_timer) { // Are We Using The Performance Timer?
QueryPerformanceCounter((LARGE_INTEGER *) &time); // Grab The Current Performance Time
// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
endTime = ( (float) ( time - performance_timer_start) * resolution)*1000.0f;
} else {
// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
//endTime = ( (float) ( timeGetTime() - mm_timer_start) * resolution)*1000.0f;
}
passTime = endTime - startTime;
}
float Timer::getTime() const
{
return passTime/1000;
}
void Timer::display() const
{
std::cout << "It takes " << passTime/1000 << " s." << std::endl;
}
#endif
- 计算时间,精确到纳秒
- 精确计算运行时间
- C 语言计算精确时间
- 精确计算代码运行时间
- 如何精确计算时间到微秒级
- 精确计算时间的C#源代码
- 精确计算程序运行的时间
- 计算程序运行时间精确到毫秒
- linux计算时间精确到微妙
- C#精确计算算法运行时间
- java计算时间差,精确到纳秒
- 如何精确计算程序运行时间——精确获取时间(QueryPerformanceCounter)
- 精确计算
- 精确计算
- python计算时间间隔(精确到微妙)
- 计算程序运行时间,精确到毫秒级
- C语言编程 计算程序运行时间 精确到微秒
- android 计算活动倒计时,时间间隔,精确到秒
- 有点郁闷!
- VS2005解决方案下无法添加项目的解决方法 (添加项目后,解决方案消失)
- jquery 截取td文本
- 2010年1月第一期图书拍拍活动预告
- phpmyadmin导入(import)文件限制的解决办法
- 计算时间,精确到纳秒
- 教你编写高性能的mysql语法
- mysql 批量修改默认值
- 1st Annual ATI Automation Honors
- AlwaysVisibleControlExtender、DragPanelExtender、ColorPickerExtender、ConfirmButtonExtender
- MMC检测到此管理单元发生一个错误。建议关闭并重新启动MMC 导致iis不可用
- select into不可乎视的问题
- HP发布了QTP的一个Patch,支持 Windows 7 和 Windows Server 2008 R2
- C语言的一些笔记