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 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.
35 // This class is not portable to non System V platforms.
36 // It will need to be rewritten for Windows, NT, Mac.
39 #ifndef _STOPWATCH_HXX
40 #define _STOPWATCH_HXX
43 # error This library requires C++
47 # include <simgear_config.h>
50 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
51 # define HAVE_GETRUSAGE
54 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
55 # undef HAVE_GETRUSAGE
58 #if defined( HAVE_GETRUSAGE )
59 # if defined( __FreeBSD__ )
60 # include <sys/types.h>
62 # include <sys/time.h>
63 # include <sys/resource.h>
65 #elif defined( WIN32 )
72 * A high resolutions timing class
79 // state_ = uninitialized;
82 /** Start counting time */
88 /** Stop counting time */
91 // BZPRECONDITION(state_ == running);
95 /** @return the elapsed time between start and stop */
96 double elapsedSeconds()
98 // BZPRECONDITION(state_ == stopped);
103 StopWatch(StopWatch&) { }
104 void operator=(StopWatch&) { }
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);
118 return clock() / (double) CLOCKS_PER_SEC;
122 // enum { uninitialized, running, stopped } state_;
124 #if defined( HAVE_GETRUSAGE )
125 struct rusage resourceUsage_;
131 #endif // _STOPWATCH_HXX