BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Timer.h
Go to the documentation of this file.
00001 // Copyright (c) 1997  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Profiling_tools/include/CGAL/Timer.h $
00019 // $Id: Timer.h 32928 2006-08-03 03:39:45Z afabri $
00020 // 
00021 //
00022 // Author(s)     : Lutz Kettner  <kettner@inf.ethz.ch>  
00023 //                 Matthias Baesken <baesken@informatik.uni-halle.de>
00024 
00025 #ifndef CGAL_TIMER_H
00026 #define CGAL_TIMER_H 1
00027 
00028 #include <CGAL/basic.h>
00029 
00030 CGAL_BEGIN_NAMESPACE
00031 
00032 // SECTION: A Timer for User-Process Time
00033 // ========================================================================
00034 // 
00035 // DEFINITION
00036 // 
00037 // A timer `t' of type Timer is an object with a state. It is either
00038 // running or it is stopped. The state is controlled with `t.start()'
00039 // and `t.stop()'. The timer counts the time elapsed since its creation
00040 // or last reset. It counts only the time where it is in the running
00041 // state. The time information is given in seconds.
00042 
00043 class Timer {
00044 private:
00045     double      elapsed;
00046     double      started;
00047     int         interv;
00048     bool        running;
00049 
00050     static bool m_failed;
00051 
00052     double   user_process_time() const; // in seconds
00053     double   compute_precision() const; // in seconds
00054 public:
00055     Timer() : elapsed(0.0), started(0.0), interv(0), running(false) {}
00056 
00057     void     start();
00058     void     stop ();
00059     void     reset();
00060     bool     is_running() const { return running; }
00061 
00062     double   time()       const;
00063     int      intervals()  const { return interv; }
00064     double   precision()  const;
00065     // Returns timer precison. Computes it dynamically at first call.
00066     // Returns -1.0 if timer system call fails, which, for a proper coded
00067     // test towards precision leads to an immediate stop of an otherwise 
00068     // infinite loop (fixed tolerance * total time >= precision).
00069     double   max BOOST_PREVENT_MACRO_SUBSTITUTION ()        const;
00070 };
00071 
00072 
00073 // -----------------------------------------------------------------------
00074 
00075 // Member functions for Timer
00076 // ===========================
00077 
00078 inline void Timer::start() {
00079     CGAL_precondition( ! running);
00080     started = user_process_time();
00081     running = true;
00082     ++ interv;
00083 }
00084 
00085 inline void Timer::stop() {
00086     CGAL_precondition( running);
00087     double t = user_process_time();
00088     elapsed += (t - started);
00089     started = 0.0;
00090     running = false;
00091 }
00092 
00093 inline void Timer::reset() {
00094     interv  = 0;
00095     elapsed = 0.0;
00096     if (running) {
00097         started = user_process_time();
00098         ++ interv;
00099     } else {
00100         started = 0.0;
00101     }
00102 }
00103 
00104 inline double Timer::time() const {
00105     if (running) {
00106         double t = user_process_time();
00107         return elapsed + (t - started);
00108     }
00109     return elapsed;
00110 }
00111 
00112 CGAL_END_NAMESPACE
00113 
00114 #endif // CGAL_TIMER_H //
00115 // EOF //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines