一个毫秒级计时的类

None.gif 1  #ifndef _GHH_TIMER_GHH_
None.gif 
2   #define  _GHH_TIMER_GHH_    1
None.gif 
3  
None.gif 
4   //  File: ghhTimer.h
None.gif
  5   //  Date: 2006.08.14
None.gif
  7  
None.gif 
8  #include  < ctime >
None.gif 
9  
None.gif
10   //  类导出导入类别的符号定义
None.gif
11  #ifdef _DLL_FILE_
None.gif
12   #define  PORTTYE __declspec(dllexport)  //  导出
None.gif
13   #else
None.gif
14   #define  PORTTYE __declspec(dllimport)  //  导入
None.gif
15   #endif      //  end of _DLL_FILE_
None.gif
16  
ExpandedBlockStart.gifContractedBlock.gif
17   /**/ /****************************************************************************
InBlock.gif18  * 类名称    ghhTimer
InBlock.gif19  * 
InBlock.gif20  * 描述
InBlock.gif21  *    本类对标准库计时函数进行了封装,可以实现非常精确的计时,毫秒级别
InBlock.gif22  *
InBlock.gif23  * 使用说明
InBlock.gif24  *    在所要计时程序段之前,调用Start函数,程序段结束时,调用Pause函数,
InBlock.gif25  *    多次调用程序段,即可以比较精确的估计程序段的运行时间
ExpandedBlockEnd.gif26 ***************************************************************************
*/

None.gif
27   class   PORTTYE ghhTimer
ExpandedBlockStart.gifContractedBlock.gif
28   dot.gif {
InBlock.gif
29 public:
InBlock.gif
30     ghhTimer();
InBlock.gif
31 
InBlock.gif
32 public:
InBlock.gif
33     bool Start(void); 
InBlock.gif
34     bool Stop(void);    
InBlock.gif
35     bool Pause(void);
InBlock.gif
36     size_t GetSeconds(voidconst;
InBlock.gif
37     size_t GetMiliSeconds(voidconst;
InBlock.gif
38 
InBlock.gif
39 private:
ExpandedSubBlockStart.gifContractedSubBlock.gif
40     enum dot.gif{run = 1, stop, pause} _Status;
InBlock.gif
41     time_t _Clock;
InBlock.gif
42     time_t _TotalClocks;
ExpandedBlockEnd.gif
43 }
;
None.gif
44  
None.gif
45   #endif   //  end of _GHH_TIMER_GHH_
None.gif   1  #ifndef _DLL_FILE_
None.gif  
2   #define  _DLL_FILE_
None.gif  
3   #endif
None.gif  
4  #include  " ghhTimer.h "
None.gif  
5  
ExpandedBlockStart.gifContractedBlock.gif  
6   /**/ /****************************************************************************
InBlock.gif  7  * about the important function "clock()"
InBlock.gif  8  * #include <time.h>
InBlock.gif  9  * clock_t clock( void );
InBlock.gif 10  * The clock() function returns the processor time since the program started, 
InBlock.gif 11  * or -1 if that information is unavailable. 
InBlock.gif 12  * To convert the return value to seconds, divide it by CLOCKS_PER_SEC. 
InBlock.gif 13  * (Note: if your compiler is POSIX compliant, 
InBlock.gif 14  * then CLOCKS_PER_SEC is always defined as 1000000.)
ExpandedBlockEnd.gif 15  **************************************************************************
*/

None.gif 
16  
None.gif 
17  
None.gif 
18   //  构造函数,设置初始状态
None.gif
  19  ghhTimer::ghhTimer() : _Status(stop), _Clock( 0 ), _TotalClocks( 0 )
ExpandedBlockStart.gifContractedBlock.gif 
20   dot.gif {
ExpandedBlockEnd.gif 
21 }

None.gif 
22  
None.gif 
23   //  当表已经停止或者暂停时启动停表,成功返回true,否则返回false
None.gif
  24   bool  ghhTimer::Start( void )
ExpandedBlockStart.gifContractedBlock.gif 
25   dot.gif {
InBlock.gif 
26     switch (_Status)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
27     dot.gif{
InBlock.gif 
28     case stop :
InBlock.gif 
29         _TotalClocks = 0;
InBlock.gif 
30         _Clock = clock();
InBlock.gif 
31         break;
InBlock.gif 
32 
InBlock.gif 
33     case pause :
InBlock.gif 
34         _Clock = clock();
InBlock.gif 
35         break;
InBlock.gif 
36 
InBlock.gif 
37     case run :
InBlock.gif 
38         break;
InBlock.gif 
39 
InBlock.gif 
40     default :
InBlock.gif 
41         return false;
ExpandedSubBlockEnd.gif 
42     }

InBlock.gif 
43 
InBlock.gif 
44     _Status = run;
InBlock.gif 
45 
InBlock.gif 
46     return true;
ExpandedBlockEnd.gif 
47 }

None.gif 
48  
None.gif 
49   //  表运行时暂停计时,成功返回true,否则返回false
None.gif
  50   bool  ghhTimer::Pause( void )
ExpandedBlockStart.gifContractedBlock.gif 
51   dot.gif {
InBlock.gif 
52     switch (_Status)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
53     dot.gif{
InBlock.gif 
54     case stop :
InBlock.gif 
55     case pause :
InBlock.gif 
56         break;
InBlock.gif 
57 
InBlock.gif 
58     case run :
InBlock.gif 
59         _TotalClocks += (clock() - _Clock);
InBlock.gif 
60         _Clock = 0;
InBlock.gif 
61         _Status = pause;
InBlock.gif 
62         break;
InBlock.gif 
63 
InBlock.gif 
64     default :
InBlock.gif 
65         return false;
ExpandedSubBlockEnd.gif 
66     }

InBlock.gif 
67 
InBlock.gif 
68     return true;
ExpandedBlockEnd.gif 
69 }

None.gif 
70  
None.gif 
71   //  表运行或暂停时停止计时
None.gif
  72   bool  ghhTimer::Stop( void )
ExpandedBlockStart.gifContractedBlock.gif 
73   dot.gif {
InBlock.gif 
74     switch (_Status)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
75     dot.gif{
InBlock.gif 
76     case stop :
InBlock.gif 
77     case pause :
InBlock.gif 
78         break;
InBlock.gif 
79         
InBlock.gif 
80     case run :
InBlock.gif 
81         _TotalClocks +=(clock() - _Clock);
InBlock.gif 
82         _Clock = 0;
InBlock.gif 
83         break;
InBlock.gif 
84 
InBlock.gif 
85     default :
InBlock.gif 
86         return false;
ExpandedSubBlockEnd.gif 
87     }

InBlock.gif 
88 
InBlock.gif 
89     _Status = stop;
InBlock.gif 
90     
InBlock.gif 
91     return true;
ExpandedBlockEnd.gif 
92 }

None.gif 
93  
None.gif 
94   //  得到当前积累的秒数
None.gif
  95  size_t ghhTimer::GetSeconds( void const
ExpandedBlockStart.gifContractedBlock.gif 
96   dot.gif {
InBlock.gif 
97     time_t Clocks;
InBlock.gif 
98 
InBlock.gif 
99     switch (_Status)
ExpandedSubBlockStart.gifContractedSubBlock.gif
100     dot.gif{
InBlock.gif
101     case stop:
InBlock.gif
102     case pause:
InBlock.gif
103         Clocks = _TotalClocks;
InBlock.gif
104         break;
InBlock.gif
105 
InBlock.gif
106     case run:
InBlock.gif
107         Clocks = _TotalClocks + clock() - _Clock;
InBlock.gif
108         break;
InBlock.gif
109 
InBlock.gif
110     default:
InBlock.gif
111         return false;
ExpandedSubBlockEnd.gif
112     }

InBlock.gif
113     return (Clocks / CLOCKS_PER_SEC);
ExpandedBlockEnd.gif
114 }

None.gif
115  
None.gif
116   //  得到当前积累的毫秒数
None.gif
117  size_t ghhTimer::GetMiliSeconds( void const
ExpandedBlockStart.gifContractedBlock.gif
118   dot.gif {
InBlock.gif
119     time_t Clocks;
InBlock.gif
120 
InBlock.gif
121     switch(_Status)
ExpandedSubBlockStart.gifContractedSubBlock.gif
122     dot.gif{
InBlock.gif
123     case stop:
InBlock.gif
124     case pause:
InBlock.gif
125         Clocks = _TotalClocks;
InBlock.gif
126         break;
InBlock.gif
127     case run:
InBlock.gif
128         Clocks = _TotalClocks + clock() - _Clock;
InBlock.gif
129         break;
InBlock.gif
130 
InBlock.gif
131     default:
InBlock.gif
132         return false;
ExpandedSubBlockEnd.gif
133     }

InBlock.gif
134     return (Clocks * 1000 / CLOCKS_PER_SEC);
ExpandedBlockEnd.gif
135 }

None.gif
136  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值