CppAD: A C++ Algorithmic Differentiation Package 20110419
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_ELAPSED_SECONDS_INCLUDED 00003 # define CPPAD_ELAPSED_SECONDS_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Common 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 # ifdef _MSC_VER 00068 extern double microsoft_timer(void); 00069 # elif CPPAD_GETTIMEOFDAY 00070 # include <sys/time.h> 00071 # else 00072 # include <ctime> 00073 # endif 00074 00075 # include <cppad/local/define.hpp> 00076 CPPAD_BEGIN_NAMESPACE 00077 /*! 00078 \file elapsed_seconds.hpp 00079 \brief Function that returns the elapsed seconds from first call. 00080 */ 00081 00082 # ifndef CPPAD_NULL 00083 # define CPPAD_NULL 0 00084 # endif 00085 00086 /*! 00087 Returns the elapsed number since the first call to this function. 00088 00089 This routine tries is accurate to within .02 seconds. 00090 It does not necessary work for time intervals that are less than a day. 00091 \li 00092 If running under the Microsoft system, it uses \c ::%GetSystemTime for timing. 00093 \li 00094 Otherwise, if \c gettimeofday is available, it is used. 00095 \li 00096 Otherwise, \c std::clock() is used. 00097 00098 \return 00099 The number of seconds since the first call to \c elapsed_seconds. 00100 */ 00101 inline double elapsed_seconds(void) 00102 # ifdef _MSC_VER 00103 { return microsoft_timer(); } 00104 00105 # elif CPPAD_GETTIMEOFDAY 00106 { static bool first_ = true; 00107 static struct timeval tv_; 00108 struct timeval tv; 00109 if( first_ ) 00110 { gettimeofday(&tv_, CPPAD_NULL); 00111 first_ = false; 00112 return 0.; 00113 } 00114 gettimeofday(&tv, CPPAD_NULL); 00115 assert( tv.tv_sec >= tv_.tv_sec ); 00116 00117 double sec = double(tv.tv_sec - tv_.tv_sec); 00118 double usec = double(tv.tv_usec) - double(tv_.tv_usec); 00119 double diff = sec + 1e-6*usec; 00120 00121 return diff; 00122 } 00123 # else 00124 { static bool first_ = true; 00125 static double tic_; 00126 double tic; 00127 if( first_ ) 00128 { tic_ = double(std::clock()); 00129 first_ = false; 00130 return 0.; 00131 } 00132 tic = double( std::clock() ); 00133 00134 double diff = (tic - tic_) / double(CLOCKS_PER_SEC); 00135 00136 return diff; 00137 } 00138 # endif 00139 00140 CPPAD_END_NAMESPACE 00141 # endif