]> git.mxchange.org Git - simgear.git/blob - simgear/misc/stopwatch.hxx
Removal of PLIB/SG from SimGear
[simgear.git] / simgear / misc / stopwatch.hxx
1 /**
2  * \file stopwatch.hxx
3  * Timer class, for use in benchmarking
4  * Based on blitz/Timer.h
5  *
6  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
7  *
8  * Suggestions:          blitz-suggest@cybervision.com
9  * Bugs:                 blitz-bugs@cybervision.com
10  *
11  * For more information, please see the Blitz++ Home Page:
12  *    http://seurat.uwaterloo.ca/blitz/
13  */
14
15 /*
16  * $Id$
17  *
18  * This library is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Library General Public
20  * License as published by the Free Software Foundation; either
21  * version 2 of the License, or (at your option) any later version.
22  *
23  * This library is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * Library General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
31  *
32  */
33
34 // This class is not portable to non System V platforms.
35 // It will need to be rewritten for Windows, NT, Mac.
36 // NEEDS_WORK
37
38 #ifndef _STOPWATCH_HXX
39 #define _STOPWATCH_HXX
40
41 #ifndef __cplusplus
42 # error This library requires C++
43 #endif
44
45 #ifdef HAVE_CONFIG_H
46 #  include <simgear_config.h>
47 #endif
48
49 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
50 #  define HAVE_GETRUSAGE
51 #endif
52
53 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
54 #  undef HAVE_GETRUSAGE
55 #endif // WIN32
56
57 #if defined( HAVE_GETRUSAGE )
58 #  if defined( __FreeBSD__ )
59 #    include <sys/types.h>
60 #  endif 
61 #  include <sys/time.h>
62 #  include <sys/resource.h>
63 #  include <unistd.h>
64 #elif defined( WIN32 )
65 #  include <windows.h>
66 #else
67 #  include <time.h>
68 #endif
69
70 /**
71  * A high resolutions timing class
72  */
73 class StopWatch {
74
75 public:
76     /** Constructor */
77     StopWatch() { 
78         // state_ = uninitialized;
79     }
80
81     /** Start counting time */
82     void start() { 
83         // state_ = running;
84         t1_ = systemTime();
85     }
86
87     /** Stop counting time */
88     void stop() {
89         t2_ = systemTime();
90         // BZPRECONDITION(state_ == running);
91         // state_ = stopped;
92     }
93
94     /** @return the elapsed time between start and stop */
95     double elapsedSeconds()
96     {
97         // BZPRECONDITION(state_ == stopped);
98         return t2_ - t1_;
99     }
100
101 private:
102     StopWatch(StopWatch&) { }
103     void operator=(StopWatch&) { }
104
105     double systemTime()
106     {
107 #if defined( HAVE_GETRUSAGE )
108         getrusage(RUSAGE_SELF, &resourceUsage_);
109         double seconds = resourceUsage_.ru_utime.tv_sec 
110             + resourceUsage_.ru_stime.tv_sec;
111         double micros  = resourceUsage_.ru_utime.tv_usec 
112             + resourceUsage_.ru_stime.tv_usec;
113         return seconds + micros/1.0e6;
114 #elif defined( WIN32 )
115         return double(GetTickCount()) * double(1e-3);
116 #else
117         return clock() / (double) CLOCKS_PER_SEC;
118 #endif
119     }
120
121 //     enum { uninitialized, running, stopped } state_;
122
123 #if defined( HAVE_GETRUSAGE )
124     struct rusage resourceUsage_;
125 #endif
126
127     double t1_, t2_;
128 };
129
130 #endif // _STOPWATCH_HXX
131