- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

timer.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33 #ifndef __INCLUDE_MIST_TIMER__
34 #define __INCLUDE_MIST_TIMER__
35
36
37 #ifndef __INCLUDE_MIST_CONF_H__
38 #include "config/mist_conf.h"
39 #endif
40
41 #if defined( __MIST_WINDOWS__ ) && __MIST_WINDOWS__ > 0
42
43 #include <windows.h>
44 #include <mmsystem.h>
45 #pragma comment ( lib, "winmm.lib" )
46
47 #else
48
49 //#include <time.h>
50 #include <sys/time.h>
51
52 #endif
53
54
55 // mist名前空間の始まり
56 _MIST_BEGIN
57
58
74 class timer
75 {
76 private:
77 #if defined( __MIST_WINDOWS__ ) && __MIST_WINDOWS__ > 0
78 union timeval
79 {
80 LARGE_INTEGER largeint;
81 unsigned long long int uint64;
82 };
83
84 // Windows の場合で，高分解能カウンタが利用可能なら利用する
85 static timeval __QueryPerformanceFrequency__( )
86 {
87 timeval dmy;
88 QueryPerformanceFrequency( &( dmy.largeint ) );
89 return( dmy );
90 }
91
92 static timeval _QueryPerformanceFrequency_( )
93 {
94 static timeval dmy = __QueryPerformanceFrequency__( );
95 return( dmy );
96 }
97
98 static double _timeGetTime_( )
99 {
100 timeval dmy;
101 if( QueryPerformanceCounter( &( dmy.largeint ) ) )
102 {
103 return( static_cast< double >( dmy.uint64 ) / static_cast< double >( _QueryPerformanceFrequency_( ).uint64 ) );
104 }
105 else
106 {
107 return( static_cast< double >( timeGetTime( ) ) / 1000.0 );
108 }
109 }
110 #else
111 static double _timeGetTime_( )
112 {
113 timeval dmy;
114 gettimeofday( &dmy, NULL );
115 return( static_cast< double >( dmy.tv_sec ) + static_cast< double >( dmy.tv_usec ) / 1000000.0 );
116 }
117 #endif
118
119 public:
124 timer( ) : _start_time( _timeGetTime_( ) )
125 {
126 _start_time = _timeGetTime_( );
127 }
128
130 void reset( )
131 {
132 _start_time = _timeGetTime_( );
133 }
134
139 double elapse( ) const
140 {
141 return( _timeGetTime_( ) - _start_time );
142 }
143
144 private:
145 double _start_time;
146 };
147
148
164 class stopwatch
165 {
166 private:
167 timer t_;
168 double sec_;
169 bool run_;
170
171 public:
176 stopwatch( ) : sec_( 0.0 ), run_( false )
177 {
178 }
179
181 void reset( )
182 {
183 t_.reset( );
184 sec_ = 0.0;
185 }
186
191 double elapse( ) const
192 {
193 if( run_ )
194 {
195 return( sec_ + t_.elapse( ) );
196 }
197 else
198 {
199 return( sec_ );
200 }
201 }
202
207 void stop( )
208 {
209 sec_ += t_.elapse( );
210 run_ = false;
211 }
212
217 void start( )
218 {
219 t_.reset( );
220 run_ = true;
221 }
222 };
223
224
232 inline ::std::ostream &operator <<( ::std::ostream &out, const timer &t )
233 {
234 out << t.elapse( );
235 return( out );
236 }
237
245 inline ::std::ostream &operator <<( ::std::ostream &out, const stopwatch &t )
246 {
247 out << t.elapse( );
248 return( out );
249 }
250
251
252 // mist名前空間の終わり
253 _MIST_END
254
255
256 #endif // __INCLUDE_MIST_TIMER__
257