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.1 1998/09/01 19:06:30 curt
31 * Revision 1.4 1998/03/14 00:04:47 tveldhui
34 * Revision 1.3 1997/07/16 14:51:20 tveldhui
35 * Update: Alpha release 0.2 (Arrays)
37 * Revision 1.2 1997/01/24 14:42:00 tveldhui
42 // This class is not portable to non System V platforms.
43 // It will need to be rewritten for Windows, NT, Mac.
46 #ifndef _STOPWATCH_HXX
47 #define _STOPWATCH_HXX
50 # error This library requires C++
57 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
58 # define HAVE_GETRUSAGE
62 #include <sys/resource.h>
72 // state_ = uninitialized;
84 // BZPRECONDITION(state_ == running);
88 double elapsedSeconds()
90 // BZPRECONDITION(state_ == stopped);
95 StopWatch(StopWatch&) { }
96 void operator=(StopWatch&) { }
100 #ifdef HAVE_GETRUSAGE
101 getrusage(RUSAGE_SELF, &resourceUsage_);
102 double seconds = resourceUsage_.ru_utime.tv_sec
103 + resourceUsage_.ru_stime.tv_sec;
104 double micros = resourceUsage_.ru_utime.tv_usec
105 + resourceUsage_.ru_stime.tv_usec;
106 return seconds + micros/1.0e6;
108 return clock() / (double) CLOCKS_PER_SEC;
112 // enum { uninitialized, running, stopped } state_;
114 #ifdef HAVE_GETRUSAGE
115 struct rusage resourceUsage_;
121 #endif // _STOPWATCH_HXX