]> git.mxchange.org Git - simgear.git/blob - simgear/misc/stopwatch.hxx
Ed Williams: Added some bulletproofing at the poles.
[simgear.git] / simgear / 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
28 // This class is not portable to non System V platforms.
29 // It will need to be rewritten for Windows, NT, Mac.
30 // NEEDS_WORK
31
32 #ifndef _STOPWATCH_HXX
33 #define _STOPWATCH_HXX
34
35 #ifndef __cplusplus                                                          
36 # error This library requires C++
37 #endif                                   
38
39 #ifdef HAVE_CONFIG_H
40 #  include <config.h>
41 #endif
42
43 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
44 #  define HAVE_GETRUSAGE
45 #endif
46
47 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
48 #  undef HAVE_GETRUSAGE
49 #endif // WIN32
50
51 #if defined( HAVE_GETRUSAGE )
52 #  if defined( __FreeBSD__ )
53 #    include <sys/types.h>
54 #  endif 
55 #  include <sys/time.h>
56 #  include <sys/resource.h>
57 #  include <unistd.h>
58 #elif defined( WIN32 )
59 #  include <windows.h>
60 #else
61 #  include <time.h>
62 #endif
63
64 class StopWatch {
65
66 public:
67     StopWatch() 
68     { 
69 //         state_ = uninitialized;
70     }
71
72     void start()
73     { 
74 //         state_ = running;
75         t1_ = systemTime();
76     }
77
78     void stop()
79     {
80         t2_ = systemTime();
81 //      BZPRECONDITION(state_ == running);
82 //      state_ = stopped;
83     }
84
85     double elapsedSeconds()
86     {
87 //         BZPRECONDITION(state_ == stopped);
88         return t2_ - t1_;
89     }
90
91 private:
92     StopWatch(StopWatch&) { }
93     void operator=(StopWatch&) { }
94
95     double systemTime()
96     {
97 #if defined( HAVE_GETRUSAGE )
98         getrusage(RUSAGE_SELF, &resourceUsage_);
99         double seconds = resourceUsage_.ru_utime.tv_sec 
100             + resourceUsage_.ru_stime.tv_sec;
101         double micros  = resourceUsage_.ru_utime.tv_usec 
102             + resourceUsage_.ru_stime.tv_usec;
103         return seconds + micros/1.0e6;
104 #elif defined( WIN32 )
105         return double(GetTickCount()) * double(1e-3);
106 #else
107         return clock() / (double) CLOCKS_PER_SEC;
108 #endif
109     }
110
111 //     enum { uninitialized, running, stopped } state_;
112
113 #if defined( HAVE_GETRUSAGE )
114     struct rusage resourceUsage_;
115 #endif
116
117     double t1_, t2_;
118 };
119
120 #endif // _STOPWATCH_HXX
121