]> git.mxchange.org Git - simgear.git/blob - simgear/misc/stopwatch.hxx
Updates to build system to better support automake-1.5
[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 Library General Public
29  * License along with this library; if not, write to the
30  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31  * Boston, MA  02111-1307, USA.
32  *
33  */
34
35 // This class is not portable to non System V platforms.
36 // It will need to be rewritten for Windows, NT, Mac.
37 // NEEDS_WORK
38
39 #ifndef _STOPWATCH_HXX
40 #define _STOPWATCH_HXX
41
42 #ifndef __cplusplus                                                          
43 # error This library requires C++
44 #endif                                   
45
46 #ifdef HAVE_CONFIG_H
47 #  include <config.h>
48 #endif
49
50 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
51 #  define HAVE_GETRUSAGE
52 #endif
53
54 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
55 #  undef HAVE_GETRUSAGE
56 #endif // WIN32
57
58 #if defined( HAVE_GETRUSAGE )
59 #  if defined( __FreeBSD__ )
60 #    include <sys/types.h>
61 #  endif 
62 #  include <sys/time.h>
63 #  include <sys/resource.h>
64 #  include <unistd.h>
65 #elif defined( WIN32 )
66 #  include <windows.h>
67 #else
68 #  include <time.h>
69 #endif
70
71 /**
72  * A high resolutions timing class
73  */
74 class StopWatch {
75
76 public:
77     /** Constructor */
78     StopWatch() { 
79         // state_ = uninitialized;
80     }
81
82     /** Start counting time */
83     void start() { 
84         // state_ = running;
85         t1_ = systemTime();
86     }
87
88     /** Stop counting time */
89     void stop() {
90         t2_ = systemTime();
91         // BZPRECONDITION(state_ == running);
92         // state_ = stopped;
93     }
94
95     /** @return the elapsed time between start and stop */
96     double elapsedSeconds()
97     {
98         // BZPRECONDITION(state_ == stopped);
99         return t2_ - t1_;
100     }
101
102 private:
103     StopWatch(StopWatch&) { }
104     void operator=(StopWatch&) { }
105
106     double systemTime()
107     {
108 #if defined( HAVE_GETRUSAGE )
109         getrusage(RUSAGE_SELF, &resourceUsage_);
110         double seconds = resourceUsage_.ru_utime.tv_sec 
111             + resourceUsage_.ru_stime.tv_sec;
112         double micros  = resourceUsage_.ru_utime.tv_usec 
113             + resourceUsage_.ru_stime.tv_usec;
114         return seconds + micros/1.0e6;
115 #elif defined( WIN32 )
116         return double(GetTickCount()) * double(1e-3);
117 #else
118         return clock() / (double) CLOCKS_PER_SEC;
119 #endif
120     }
121
122 //     enum { uninitialized, running, stopped } state_;
123
124 #if defined( HAVE_GETRUSAGE )
125     struct rusage resourceUsage_;
126 #endif
127
128     double t1_, t2_;
129 };
130
131 #endif // _STOPWATCH_HXX
132