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