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.3 1998/11/20 01:01:03 curt
31 * Revision 1.2 1998/11/02 18:28:31 curt
32 * Additional win32 support.
34 * Revision 1.1 1998/09/01 19:06:30 curt
37 * Revision 1.4 1998/03/14 00:04:47 tveldhui
40 * Revision 1.3 1997/07/16 14:51:20 tveldhui
41 * Update: Alpha release 0.2 (Arrays)
43 * Revision 1.2 1997/01/24 14:42:00 tveldhui
48 // This class is not portable to non System V platforms.
49 // It will need to be rewritten for Windows, NT, Mac.
52 #ifndef _STOPWATCH_HXX
53 #define _STOPWATCH_HXX
56 # error This library requires C++
63 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
64 # define HAVE_GETRUSAGE
67 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
68 # undef HAVE_GETRUSAGE
71 #if defined( HAVE_GETRUSAGE )
72 # if defined( __FreeBSD__ )
73 # include <sys/types.h>
75 # include <sys/time.h>
76 # include <sys/resource.h>
78 #elif defined( WIN32 )
89 // state_ = uninitialized;
101 // BZPRECONDITION(state_ == running);
105 double elapsedSeconds()
107 // BZPRECONDITION(state_ == stopped);
112 StopWatch(StopWatch&) { }
113 void operator=(StopWatch&) { }
117 #if defined( HAVE_GETRUSAGE )
118 getrusage(RUSAGE_SELF, &resourceUsage_);
119 double seconds = resourceUsage_.ru_utime.tv_sec
120 + resourceUsage_.ru_stime.tv_sec;
121 double micros = resourceUsage_.ru_utime.tv_usec
122 + resourceUsage_.ru_stime.tv_usec;
123 return seconds + micros/1.0e6;
124 #elif defined( WIN32 )
125 return double(GetTickCount()) * double(1e-3);
127 return clock() / (double) CLOCKS_PER_SEC;
131 // enum { uninitialized, running, stopped } state_;
133 #if defined( HAVE_GETRUSAGE )
134 struct rusage resourceUsage_;
140 #endif // _STOPWATCH_HXX