]> git.mxchange.org Git - simgear.git/blob - simgear/misc/stopwatch.hxx
Updates from David Megginson:
[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 library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library 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 GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA  02111-1307, USA.
24  *
25  * Suggestions:          blitz-suggest@cybervision.com
26  * Bugs:                 blitz-bugs@cybervision.com
27  *
28  * For more information, please see the Blitz++ Home Page:
29  *    http://seurat.uwaterloo.ca/blitz/
30  *
31  */
32
33 // This class is not portable to non System V platforms.
34 // It will need to be rewritten for Windows, NT, Mac.
35 // NEEDS_WORK
36
37 #ifndef _STOPWATCH_HXX
38 #define _STOPWATCH_HXX
39
40 #ifndef __cplusplus                                                          
41 # error This library requires C++
42 #endif                                   
43
44 #ifdef HAVE_CONFIG_H
45 #  include <config.h>
46 #endif
47
48 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
49 #  define HAVE_GETRUSAGE
50 #endif
51
52 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
53 #  undef HAVE_GETRUSAGE
54 #endif // WIN32
55
56 #if defined( HAVE_GETRUSAGE )
57 #  if defined( __FreeBSD__ )
58 #    include <sys/types.h>
59 #  endif 
60 #  include <sys/time.h>
61 #  include <sys/resource.h>
62 #  include <unistd.h>
63 #elif defined( WIN32 )
64 #  include <windows.h>
65 #else
66 #  include <time.h>
67 #endif
68
69 class StopWatch {
70
71 public:
72     StopWatch() 
73     { 
74 //         state_ = uninitialized;
75     }
76
77     void start()
78     { 
79 //         state_ = running;
80         t1_ = systemTime();
81     }
82
83     void stop()
84     {
85         t2_ = systemTime();
86 //      BZPRECONDITION(state_ == running);
87 //      state_ = stopped;
88     }
89
90     double elapsedSeconds()
91     {
92 //         BZPRECONDITION(state_ == stopped);
93         return t2_ - t1_;
94     }
95
96 private:
97     StopWatch(StopWatch&) { }
98     void operator=(StopWatch&) { }
99
100     double systemTime()
101     {
102 #if defined( HAVE_GETRUSAGE )
103         getrusage(RUSAGE_SELF, &resourceUsage_);
104         double seconds = resourceUsage_.ru_utime.tv_sec 
105             + resourceUsage_.ru_stime.tv_sec;
106         double micros  = resourceUsage_.ru_utime.tv_usec 
107             + resourceUsage_.ru_stime.tv_usec;
108         return seconds + micros/1.0e6;
109 #elif defined( WIN32 )
110         return double(GetTickCount()) * double(1e-3);
111 #else
112         return clock() / (double) CLOCKS_PER_SEC;
113 #endif
114     }
115
116 //     enum { uninitialized, running, stopped } state_;
117
118 #if defined( HAVE_GETRUSAGE )
119     struct rusage resourceUsage_;
120 #endif
121
122     double t1_, t2_;
123 };
124
125 #endif // _STOPWATCH_HXX
126