1 /* 2 * 微秒级计时器,用来统计程序运行时间 3 * http://blog.csdn.net/hoya5121/article/details/3778487#comments 4 * //整理 [10/16/2013 Duan Yihao] 5 */ 6 7 #pragma once 8 9 #include "stdafx.h" 10 11 12 ////////////////////////////////////////////////////////////////////////// 13 class timer 14 { 15 public: 16 timer(); 17 ~timer(); 18 19 void start(void); 20 21 void end(void); 22 23 DWORD getTime(void) const; 24 25 private: 26 LARGE_INTEGER m_i64CPUFreq; 27 LARGE_INTEGER m_i64Begin; 28 LARGE_INTEGER m_i64End; 29 void reset(void); 30 }; 31 32 33 ////////////////////////////////////////////////////////////////////////// 34 timer::timer() 35 { 36 QueryPerformanceFrequency(&m_i64CPUFreq); 37 } 38 39 timer::~timer() 40 { 41 42 } 43 44 void timer::start(void) 45 { 46 reset(); 47 QueryPerformanceCounter(&m_i64Begin); 48 } 49 50 void timer::end(void) 51 { 52 QueryPerformanceCounter(&m_i64End); 53 } 54 55 DWORD timer::getTime(void) const 56 { 57 DWORD dwTotalMicrosecond = 0; 58 LARGE_INTEGER i64TickCount; 59 i64TickCount.QuadPart = m_i64End.QuadPart - m_i64Begin.QuadPart; 60 dwTotalMicrosecond = (DWORD) (i64TickCount.QuadPart * 1000000 / m_i64CPUFreq.QuadPart); 61 62 return dwTotalMicrosecond; 63 } 64 65 void timer::reset(void) 66 { 67 m_i64Begin = m_i64End; 68 } 69 70 71 /* 72 ////////////////////////////////////////////////////////////////////////// 73 //MFC下使用举例: 74 75 void CtestMFCView::OnTest() 76 { 77 78 // TODO: 在此添加命令处理程序代码 79 //测试例子 80 81 //计时开始 82 timer t; 83 t.start(); 84 85 //运算过程 86 87 for(int i = 0; i < 10; i++) 88 { 89 int n = i; 90 for(int i = 0; i < 10000; i++) 91 { 92 int n = i; 93 } 94 } 95 96 //计时结束 97 t.end(); 98 99 //输出运行时间 100 CString s; 101 s.Format(_T("%u ms"), t.getTime()); 102 AfxMessageBox(s); 103 } 104 105 */