3 * Timer class, for use in benchmarking
4 * Based on blitz/Timer.h
6 * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
8 * Suggestions: blitz-suggest@cybervision.com
9 * Bugs: blitz-bugs@cybervision.com
11 * For more information, please see the Blitz++ Home Page:
12 * http://seurat.uwaterloo.ca/blitz/
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.
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.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 // This class is not portable to non System V platforms.
35 // It will need to be rewritten for Windows, NT, Mac.
38 #ifndef _STOPWATCH_HXX
39 #define _STOPWATCH_HXX
42 # error This library requires C++
46 # include <simgear_config.h>
49 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
50 # define HAVE_GETRUSAGE
53 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
54 # undef HAVE_GETRUSAGE
57 #if defined( HAVE_GETRUSAGE )
58 # if defined( __FreeBSD__ )
59 # include <sys/types.h>
61 # include <sys/time.h>
62 # include <sys/resource.h>
64 #elif defined( WIN32 )
71 * A high resolutions timing class
78 // state_ = uninitialized;
81 /** Start counting time */
87 /** Stop counting time */
90 // BZPRECONDITION(state_ == running);
94 /** @return the elapsed time between start and stop */
95 double elapsedSeconds()
97 // BZPRECONDITION(state_ == stopped);
102 StopWatch(StopWatch&) { }
103 void operator=(StopWatch&) { }
107 #if defined( HAVE_GETRUSAGE )
108 getrusage(RUSAGE_SELF, &resourceUsage_);
109 double seconds = resourceUsage_.ru_utime.tv_sec
110 + resourceUsage_.ru_stime.tv_sec;
111 double micros = resourceUsage_.ru_utime.tv_usec
112 + resourceUsage_.ru_stime.tv_usec;
113 return seconds + micros/1.0e6;
114 #elif defined( WIN32 )
115 return double(GetTickCount()) * double(1e-3);
117 return clock() / (double) CLOCKS_PER_SEC;
121 // enum { uninitialized, running, stopped } state_;
123 #if defined( HAVE_GETRUSAGE )
124 struct rusage resourceUsage_;
130 #endif // _STOPWATCH_HXX