]> git.mxchange.org Git - simgear.git/blob - Misc/stopwatch.hxx
Added an FG_SERIAL type to the FG_LOG macro.
[simgear.git] / 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.2  1998/11/02 18:28:31  curt
29  * Additional win32 support.
30  *
31  * Revision 1.1  1998/09/01 19:06:30  curt
32  * Initial revision.
33  *
34  * Revision 1.4  1998/03/14 00:04:47  tveldhui
35  * 0.2-alpha-05
36  *
37  * Revision 1.3  1997/07/16 14:51:20  tveldhui
38  * Update: Alpha release 0.2 (Arrays)
39  *
40  * Revision 1.2  1997/01/24 14:42:00  tveldhui
41  * Periodic RCS update
42  *
43  */
44
45 // This class is not portable to non System V platforms.
46 // It will need to be rewritten for Windows, NT, Mac.
47 // NEEDS_WORK
48
49 #ifndef _STOPWATCH_HXX
50 #define _STOPWATCH_HXX
51
52 #ifndef __cplusplus                                                          
53 # error This library requires C++
54 #endif                                   
55
56 #ifdef HAVE_CONFIG_H
57 #  include "config.h"
58 #endif
59
60 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
61 #  define HAVE_GETRUSAGE
62 #endif
63
64 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
65 #  undef HAVE_GETRUSAGE
66 #endif // WIN32
67
68 #if defined( HAVE_GETRUSAGE )
69 #  include <sys/resource.h>
70 #elif defined( WIN32 )
71 #  include <windows.h>
72 #else
73 #  include <time.h>
74 #endif
75
76 class StopWatch {
77
78 public:
79     StopWatch() 
80     { 
81 //         state_ = uninitialized;
82     }
83
84     void start()
85     { 
86 //         state_ = running;
87         t1_ = systemTime();
88     }
89
90     void stop()
91     {
92         t2_ = systemTime();
93 //      BZPRECONDITION(state_ == running);
94 //      state_ = stopped;
95     }
96
97     double elapsedSeconds()
98     {
99 //         BZPRECONDITION(state_ == stopped);
100         return t2_ - t1_;
101     }
102
103 private:
104     StopWatch(StopWatch&) { }
105     void operator=(StopWatch&) { }
106
107     double systemTime()
108     {
109 #if defined( HAVE_GETRUSAGE )
110         getrusage(RUSAGE_SELF, &resourceUsage_);
111         double seconds = resourceUsage_.ru_utime.tv_sec 
112             + resourceUsage_.ru_stime.tv_sec;
113         double micros  = resourceUsage_.ru_utime.tv_usec 
114             + resourceUsage_.ru_stime.tv_usec;
115         return seconds + micros/1.0e6;
116 #elif defined( WIN32 )
117         return double(GetTickCount()) * double(1e-3);
118 #else
119         return clock() / (double) CLOCKS_PER_SEC;
120 #endif
121     }
122
123 //     enum { uninitialized, running, stopped } state_;
124
125 #if defined( HAVE_GETRUSAGE )
126     struct rusage resourceUsage_;
127 #endif
128
129     double t1_, t2_;
130 };
131
132 #endif // _STOPWATCH_HXX
133