head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.41; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.58; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.05; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.01; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# stopwatch.hxx

Go to the documentation of this file.

    00001
    00015 /*
    00016  * $Id: stopwatch.hxx,v 1.3 2001/03/25 12:45:13 curt Exp $
    00017  *
    00018  * This library is free software; you can redistribute it and/or
    00019  * modify it under the terms of the GNU Library General Public
    00020  * License as published by the Free Software Foundation; either
    00021  * version 2 of the License, or (at your option) any later version.
    00022  *
    00023  * This library is distributed in the hope that it will be useful,
    00024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00026  * Library General Public License for more details.
    00027  *
    00028  * You should have received a copy of the GNU Library General Public
    00029  * License along with this library; if not, write to the
    00030  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00031  * Boston, MA  02111-1307, USA.
    00032  *
    00033  */
    00034
    00035 // This class is not portable to non System V platforms.
    00036 // It will need to be rewritten for Windows, NT, Mac.
    00037 // NEEDS_WORK
    00038
    00039 #ifndef _STOPWATCH_HXX
    00040 #define _STOPWATCH_HXX
    00041
    00042 #ifndef __cplusplus
    00043 # error This library requires C++
    00044 #endif
    00045
    00046 #ifdef HAVE_CONFIG_H
    00047 #  include <config.h>
    00048 #endif
    00049
    00050 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
    00051 #  define HAVE_GETRUSAGE
    00052 #endif
    00053
    00054 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
    00055 #  undef HAVE_GETRUSAGE
    00056 #endif // WIN32
    00057
    00058 #if defined( HAVE_GETRUSAGE )
    00059 #  if defined( __FreeBSD__ )
    00060 #    include <sys/types.h>
    00061 #  endif
    00062 #  include <sys/time.h>
    00063 #  include <sys/resource.h>
    00064 #  include <unistd.h>
    00065 #elif defined( WIN32 )
    00066 #  include <windows.h>
    00067 #else
    00068 #  include <time.h>
    00069 #endif
    00070
    00074 class StopWatch {
    00075
    00076 public:
    00078     StopWatch() {
    00079         // state_ = uninitialized;
    00080     }
    00081
    00083     void start() {
    00084         // state_ = running;
    00085         t1_ = systemTime();
    00086     }
    00087
    00089     void stop() {
    00090         t2_ = systemTime();
    00091         // BZPRECONDITION(state_ == running);
    00092         // state_ = stopped;
    00093     }
    00094
    00096     double elapsedSeconds()
    00097     {
    00098         // BZPRECONDITION(state_ == stopped);
    00099         return t2_ - t1_;
    00100     }
    00101
    00102 private:
    00103     StopWatch(StopWatch&) { }
    00104     void operator=(StopWatch&) { }
    00105
    00106     double systemTime()
    00107     {
    00108 #if defined( HAVE_GETRUSAGE )
    00109         getrusage(RUSAGE_SELF, &resourceUsage_);
    00110         double seconds = resourceUsage_.ru_utime.tv_sec
    00111             + resourceUsage_.ru_stime.tv_sec;
    00112         double micros  = resourceUsage_.ru_utime.tv_usec
    00113             + resourceUsage_.ru_stime.tv_usec;
    00114         return seconds + micros/1.0e6;
    00115 #elif defined( WIN32 )
    00116         return double(GetTickCount()) * double(1e-3);
    00117 #else
    00118         return clock() / (double) CLOCKS_PER_SEC;
    00119 #endif
    00120     }
    00121
    00122 //     enum { uninitialized, running, stopped } state_;
    00123
    00124 #if defined( HAVE_GETRUSAGE )
    00125     struct rusage resourceUsage_;
    00126 #endif
    00127
    00128     double t1_, t2_;
    00129 };
    00130
    00131 #endif // _STOPWATCH_HXX
    00132

@ 1.3 log @Oops, try again. @ text @d121 1 a121 1