]> git.mxchange.org Git - flightgear.git/blob - Lib/Misc/stopwatch.hxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / Misc / stopwatch.hxx
1 /***************************************************************************
2  * stopwatch.hxx        Timer class, for use in benchmarking
3  *
4  * Based on blitz/Timer.h
5  *
6  * $Id$
7  *
8  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * Suggestions:          blitz-suggest@cybervision.com
21  * Bugs:                 blitz-bugs@cybervision.com
22  *
23  * For more information, please see the Blitz++ Home Page:
24  *    http://seurat.uwaterloo.ca/blitz/
25  *
26  ***************************************************************************
27  * $Log$
28  * Revision 1.3  1998/11/20 01:01:03  curt
29  * FreeBSD support.
30  *
31  * Revision 1.2  1998/11/02 18:28:31  curt
32  * Additional win32 support.
33  *
34  * Revision 1.1  1998/09/01 19:06:30  curt
35  * Initial revision.
36  *
37  * Revision 1.4  1998/03/14 00:04:47  tveldhui
38  * 0.2-alpha-05
39  *
40  * Revision 1.3  1997/07/16 14:51:20  tveldhui
41  * Update: Alpha release 0.2 (Arrays)
42  *
43  * Revision 1.2  1997/01/24 14:42:00  tveldhui
44  * Periodic RCS update
45  *
46  */
47
48 // This class is not portable to non System V platforms.
49 // It will need to be rewritten for Windows, NT, Mac.
50 // NEEDS_WORK
51
52 #ifndef _STOPWATCH_HXX
53 #define _STOPWATCH_HXX
54
55 #ifndef __cplusplus                                                          
56 # error This library requires C++
57 #endif                                   
58
59 #ifdef HAVE_CONFIG_H
60 #  include "config.h"
61 #endif
62
63 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
64 #  define HAVE_GETRUSAGE
65 #endif
66
67 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
68 #  undef HAVE_GETRUSAGE
69 #endif // WIN32
70
71 #if defined( HAVE_GETRUSAGE )
72 #  if defined( __FreeBSD__ )
73 #    include <sys/types.h>
74 #  endif 
75 #  include <sys/time.h>
76 #  include <sys/resource.h>
77 #  include <unistd.h>
78 #elif defined( WIN32 )
79 #  include <windows.h>
80 #else
81 #  include <time.h>
82 #endif
83
84 class StopWatch {
85
86 public:
87     StopWatch() 
88     { 
89 //         state_ = uninitialized;
90     }
91
92     void start()
93     { 
94 //         state_ = running;
95         t1_ = systemTime();
96     }
97
98     void stop()
99     {
100         t2_ = systemTime();
101 //      BZPRECONDITION(state_ == running);
102 //      state_ = stopped;
103     }
104
105     double elapsedSeconds()
106     {
107 //         BZPRECONDITION(state_ == stopped);
108         return t2_ - t1_;
109     }
110
111 private:
112     StopWatch(StopWatch&) { }
113     void operator=(StopWatch&) { }
114
115     double systemTime()
116     {
117 #if defined( HAVE_GETRUSAGE )
118         getrusage(RUSAGE_SELF, &resourceUsage_);
119         double seconds = resourceUsage_.ru_utime.tv_sec 
120             + resourceUsage_.ru_stime.tv_sec;
121         double micros  = resourceUsage_.ru_utime.tv_usec 
122             + resourceUsage_.ru_stime.tv_usec;
123         return seconds + micros/1.0e6;
124 #elif defined( WIN32 )
125         return double(GetTickCount()) * double(1e-3);
126 #else
127         return clock() / (double) CLOCKS_PER_SEC;
128 #endif
129     }
130
131 //     enum { uninitialized, running, stopped } state_;
132
133 #if defined( HAVE_GETRUSAGE )
134     struct rusage resourceUsage_;
135 #endif
136
137     double t1_, t2_;
138 };
139
140 #endif // _STOPWATCH_HXX
141