1 /***************************************************************************
2 * stopwatch.hxx Timer class, for use in benchmarking
4 * Based on blitz/Timer.h
8 * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
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.
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.
20 * Suggestions: blitz-suggest@cybervision.com
21 * Bugs: blitz-bugs@cybervision.com
23 * For more information, please see the Blitz++ Home Page:
24 * http://seurat.uwaterloo.ca/blitz/
26 ***************************************************************************
28 * Revision 1.2 1998/11/02 18:28:31 curt
29 * Additional win32 support.
31 * Revision 1.1 1998/09/01 19:06:30 curt
34 * Revision 1.4 1998/03/14 00:04:47 tveldhui
37 * Revision 1.3 1997/07/16 14:51:20 tveldhui
38 * Update: Alpha release 0.2 (Arrays)
40 * Revision 1.2 1997/01/24 14:42:00 tveldhui
45 // This class is not portable to non System V platforms.
46 // It will need to be rewritten for Windows, NT, Mac.
49 #ifndef _STOPWATCH_HXX
50 #define _STOPWATCH_HXX
53 # error This library requires C++
60 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
61 # define HAVE_GETRUSAGE
64 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
65 # undef HAVE_GETRUSAGE
68 #if defined( HAVE_GETRUSAGE )
69 # include <sys/resource.h>
70 #elif defined( WIN32 )
81 // state_ = uninitialized;
93 // BZPRECONDITION(state_ == running);
97 double elapsedSeconds()
99 // BZPRECONDITION(state_ == stopped);
104 StopWatch(StopWatch&) { }
105 void operator=(StopWatch&) { }
109 #if defined( HAVE_GETRUSAGE )
110 getrusage(RUSAGE_SELF, &resourceUsage_);
111 double seconds = resourceUsage_.ru_utime.tv_sec
112 + resourceUsage_.ru_stime.tv_sec;
113 double micros = resourceUsage_.ru_utime.tv_usec
114 + resourceUsage_.ru_stime.tv_usec;
115 return seconds + micros/1.0e6;
116 #elif defined( WIN32 )
117 return double(GetTickCount()) * double(1e-3);
119 return clock() / (double) CLOCKS_PER_SEC;
123 // enum { uninitialized, running, stopped } state_;
125 #if defined( HAVE_GETRUSAGE )
126 struct rusage resourceUsage_;
132 #endif // _STOPWATCH_HXX