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/
28 // This class is not portable to non System V platforms.
29 // It will need to be rewritten for Windows, NT, Mac.
32 #ifndef _STOPWATCH_HXX
33 #define _STOPWATCH_HXX
36 # error This library requires C++
43 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
44 # define HAVE_GETRUSAGE
47 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
48 # undef HAVE_GETRUSAGE
51 #if defined( HAVE_GETRUSAGE )
52 # if defined( __FreeBSD__ )
53 # include <sys/types.h>
55 # include <sys/time.h>
56 # include <sys/resource.h>
58 #elif defined( WIN32 )
69 // state_ = uninitialized;
81 // BZPRECONDITION(state_ == running);
85 double elapsedSeconds()
87 // BZPRECONDITION(state_ == stopped);
92 StopWatch(StopWatch&) { }
93 void operator=(StopWatch&) { }
97 #if defined( HAVE_GETRUSAGE )
98 getrusage(RUSAGE_SELF, &resourceUsage_);
99 double seconds = resourceUsage_.ru_utime.tv_sec
100 + resourceUsage_.ru_stime.tv_sec;
101 double micros = resourceUsage_.ru_utime.tv_usec
102 + resourceUsage_.ru_stime.tv_usec;
103 return seconds + micros/1.0e6;
104 #elif defined( WIN32 )
105 return double(GetTickCount()) * double(1e-3);
107 return clock() / (double) CLOCKS_PER_SEC;
111 // enum { uninitialized, running, stopped } state_;
113 #if defined( HAVE_GETRUSAGE )
114 struct rusage resourceUsage_;
120 #endif // _STOPWATCH_HXX