CppAD: A C++ Algorithmic Differentiation Package
20130102
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_ELAPSED_SECONDS_INCLUDED 00003 # define CPPAD_ELAPSED_SECONDS_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 00016 /* 00017 $begin elapsed_seconds$$ 00018 $spell 00019 Microsoft 00020 gettimeofday 00021 std 00022 $$ 00023 00024 $section Returns Elapsed Number of Seconds$$ 00025 $index elapsed_seconds$$ 00026 $index seconds, time$$ 00027 $index time, seconds$$ 00028 00029 00030 $head Syntax$$ 00031 $icode%s% = elapsed_seconds()%$$ 00032 00033 $head Purpose$$ 00034 This routine is accurate to within .02 seconds 00035 (see $cref elapsed_seconds.cpp$$). 00036 It does not necessary work for time intervals that are greater than a day. 00037 $list number$$ 00038 If running under the Microsoft compiler, it uses 00039 $code ::GetSystemTime$$ for timing. 00040 $lnext 00041 Otherwise, if $code gettimeofday$$ is available, it is used. 00042 $lnext 00043 Otherwise, $code std::clock()$$ is used. 00044 $lend 00045 00046 $head s$$ 00047 is a $code double$$ equal to the 00048 number of seconds since the first call to $code elapsed_seconds$$. 00049 00050 $head Microsoft Systems$$ 00051 It you are using the Microsoft C++ compiler, 00052 you will need to link in the external routine 00053 called $cref microsoft_timer$$. 00054 00055 $children% 00056 speed/example/elapsed_seconds.cpp 00057 %$$ 00058 $head Example$$ 00059 The routine $cref elapsed_seconds.cpp$$ is 00060 an example and test of this routine. 00061 00062 00063 $end 00064 ----------------------------------------------------------------------- 00065 */ 00066 00067 // For some unknown reason under Fedora (which needs to be understood), 00068 // if you move this include for cppad_assert.hpp below include for define.hpp, 00069 // cd work/speed/example 00070 // make test.sh 00071 // fails with the error message 'gettimeofday' not defined. 00072 # include <cppad/local/cppad_assert.hpp> 00073 00074 # ifdef _MSC_VER 00075 extern double microsoft_timer(void); 00076 # elif CPPAD_GETTIMEOFDAY 00077 # include <sys/time.h> 00078 # else 00079 # include <ctime> 00080 # endif 00081 00082 // define CPPAD_NULL 00083 # include <cppad/local/define.hpp> 00084 00085 // needed before one can use CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL 00086 # include <cppad/thread_alloc.hpp> 00087 CPPAD_BEGIN_NAMESPACE 00088 /*! 00089 \defgroup elapsed_seconds_hpp elapsed_seconds.hpp 00090 \{ 00091 \file elapsed_seconds.hpp 00092 \brief Function that returns the elapsed seconds from first call. 00093 */ 00094 00095 /*! 00096 Returns the elapsed number since the first call to this function. 00097 00098 This routine tries is accurate to within .02 seconds. 00099 It does not necessary work for time intervals that are less than a day. 00100 \li 00101 If running under the Microsoft system, it uses \c ::%GetSystemTime for timing. 00102 \li 00103 Otherwise, if \c gettimeofday is available, it is used. 00104 \li 00105 Otherwise, \c std::clock() is used. 00106 00107 \return 00108 The number of seconds since the first call to \c elapsed_seconds. 00109 */ 00110 inline double elapsed_seconds(void) 00111 # ifdef _MSC_VER 00112 { return microsoft_timer(); } 00113 00114 # elif CPPAD_GETTIMEOFDAY 00115 { CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL; 00116 static bool first_ = true; 00117 static struct timeval tv_; 00118 struct timeval tv; 00119 if( first_ ) 00120 { gettimeofday(&tv_, CPPAD_NULL); 00121 first_ = false; 00122 return 0.; 00123 } 00124 gettimeofday(&tv, CPPAD_NULL); 00125 assert( tv.tv_sec >= tv_.tv_sec ); 00126 00127 double sec = double(tv.tv_sec - tv_.tv_sec); 00128 double usec = double(tv.tv_usec) - double(tv_.tv_usec); 00129 double diff = sec + 1e-6*usec; 00130 00131 return diff; 00132 } 00133 # else 00134 { CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL; 00135 static bool first_ = true; 00136 static double tic_; 00137 double tic; 00138 if( first_ ) 00139 { tic_ = double(std::clock()); 00140 first_ = false; 00141 return 0.; 00142 } 00143 tic = double( std::clock() ); 00144 00145 double diff = (tic - tic_) / double(CLOCKS_PER_SEC); 00146 00147 return diff; 00148 } 00149 # endif 00150 00151 /*! \} */ 00152 CPPAD_END_NAMESPACE 00153 # endif