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 library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library 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 GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
25 * Suggestions: blitz-suggest@cybervision.com
26 * Bugs: blitz-bugs@cybervision.com
28 * For more information, please see the Blitz++ Home Page:
29 * http://seurat.uwaterloo.ca/blitz/
33 // This class is not portable to non System V platforms.
34 // It will need to be rewritten for Windows, NT, Mac.
37 #ifndef _STOPWATCH_HXX
38 #define _STOPWATCH_HXX
41 # error This library requires C++
48 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
49 # define HAVE_GETRUSAGE
52 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
53 # undef HAVE_GETRUSAGE
56 #if defined( HAVE_GETRUSAGE )
57 # if defined( __FreeBSD__ )
58 # include <sys/types.h>
60 # include <sys/time.h>
61 # include <sys/resource.h>
63 #elif defined( WIN32 )
74 // state_ = uninitialized;
86 // BZPRECONDITION(state_ == running);
90 double elapsedSeconds()
92 // BZPRECONDITION(state_ == stopped);
97 StopWatch(StopWatch&) { }
98 void operator=(StopWatch&) { }
102 #if defined( HAVE_GETRUSAGE )
103 getrusage(RUSAGE_SELF, &resourceUsage_);
104 double seconds = resourceUsage_.ru_utime.tv_sec
105 + resourceUsage_.ru_stime.tv_sec;
106 double micros = resourceUsage_.ru_utime.tv_usec
107 + resourceUsage_.ru_stime.tv_usec;
108 return seconds + micros/1.0e6;
109 #elif defined( WIN32 )
110 return double(GetTickCount()) * double(1e-3);
112 return clock() / (double) CLOCKS_PER_SEC;
116 // enum { uninitialized, running, stopped } state_;
118 #if defined( HAVE_GETRUSAGE )
119 struct rusage resourceUsage_;
125 #endif // _STOPWATCH_HXX