CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_ELAPSED_SECONDS_INCLUDED 00003 # define CPPAD_ELAPSED_SECONDS_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 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_HAS_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 namespace CppAD { // BEGIN_CPPAD_NAMESPACE 00088 /*! 00089 \file elapsed_seconds.hpp 00090 \brief Function that returns the elapsed seconds from first call. 00091 */ 00092 00093 /*! 00094 Returns the elapsed number since the first call to this function. 00095 00096 This routine tries is accurate to within .02 seconds. 00097 It does not necessary work for time intervals that are less than a day. 00098 \li 00099 If running under the Microsoft system, it uses \c ::%GetSystemTime for timing. 00100 \li 00101 Otherwise, if \c gettimeofday is available, it is used. 00102 \li 00103 Otherwise, \c std::clock() is used. 00104 00105 \return 00106 The number of seconds since the first call to \c elapsed_seconds. 00107 */ 00108 inline double elapsed_seconds(void) 00109 # ifdef _MSC_VER 00110 { return microsoft_timer(); } 00111 00112 # elif CPPAD_HAS_GETTIMEOFDAY 00113 { CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL; 00114 static bool first_ = true; 00115 static struct timeval tv_; 00116 struct timeval tv; 00117 if( first_ ) 00118 { gettimeofday(&tv_, CPPAD_NULL); 00119 first_ = false; 00120 return 0.; 00121 } 00122 gettimeofday(&tv, CPPAD_NULL); 00123 assert( tv.tv_sec >= tv_.tv_sec ); 00124 00125 double sec = double(tv.tv_sec - tv_.tv_sec); 00126 double usec = double(tv.tv_usec) - double(tv_.tv_usec); 00127 double diff = sec + 1e-6*usec; 00128 00129 return diff; 00130 } 00131 # else 00132 { CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL; 00133 static bool first_ = true; 00134 static double tic_; 00135 double tic; 00136 if( first_ ) 00137 { tic_ = double(std::clock()); 00138 first_ = false; 00139 return 0.; 00140 } 00141 tic = double( std::clock() ); 00142 00143 double diff = (tic - tic_) / double(CLOCKS_PER_SEC); 00144 00145 return diff; 00146 } 00147 # endif 00148 00149 } // END_CPPAD_NAMESPACE 00150 # endif