CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
elapsed_seconds.hpp
Go to the documentation of this file.
1 // $Id: elapsed_seconds.hpp 3845 2016-11-19 01:50:47Z bradbell $
2 # ifndef CPPAD_UTILITY_ELAPSED_SECONDS_HPP
3 # define CPPAD_UTILITY_ELAPSED_SECONDS_HPP
4 
5 /* --------------------------------------------------------------------------
6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
7 
8 CppAD is distributed under multiple licenses. This distribution is under
9 the terms of the
10  Eclipse Public License Version 1.0.
11 
12 A copy of this license is included in the COPYING file of this distribution.
13 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14 -------------------------------------------------------------------------- */
15 
16 /*
17 $begin elapsed_seconds$$
18 $spell
19  cppad.hpp
20  Microsoft
21  gettimeofday
22  std
23  chrono
24 $$
25 
26 $section Returns Elapsed Number of Seconds$$
27 $mindex elapsed_seconds time$$
28 
29 
30 $head Syntax$$
31 $codei%# include <cppad/utility/elapsed_seconds.hpp>
32 %$$
33 $icode%s% = elapsed_seconds()%$$
34 
35 $head Purpose$$
36 This routine is accurate to within .02 seconds
37 (see $cref elapsed_seconds.cpp$$).
38 It does not necessary work for time intervals that are greater than a day.
39 $list number$$
40 If the C++11 $code std::chrono::steady_clock$$ is available,
41 it will be used for timing.
42 $lnext
43 Otherwise, if running under the Microsoft compiler,
44 $code ::GetSystemTime$$ will be used for timing.
45 $lnext
46 Otherwise, if $code gettimeofday$$ is available, it is used for timing.
47 $lnext
48 Otherwise, $code std::clock()$$ will be used for timing.
49 $lend
50 
51 $head s$$
52 is a $code double$$ equal to the
53 number of seconds since the first call to $code elapsed_seconds$$.
54 
55 $head Microsoft Systems$$
56 It you are using $code ::GetSystemTime$$,
57 you will need to link in the external routine
58 called $cref microsoft_timer$$.
59 
60 $children%
61  speed/example/elapsed_seconds.cpp
62 %$$
63 $head Example$$
64 The routine $cref elapsed_seconds.cpp$$ is
65 an example and test of this routine.
66 
67 
68 $end
69 -----------------------------------------------------------------------
70 */
71 
72 // For some unknown reason under Fedora (which needs to be understood),
73 // if you move this include for cppad_assert.hpp below include for define.hpp,
74 // cd work/speed/example
75 // make test.sh
76 // fails with the error message 'gettimeofday' not defined.
78 
79 // define CPPAD_NULL
80 # include <cppad/core/define.hpp>
81 
82 // needed before one can use CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
84 
85 # if CPPAD_USE_CPLUSPLUS_2011
86 # include <chrono>
87 # elif _MSC_VER
88 extern double microsoft_timer(void);
89 # elif CPPAD_HAS_GETTIMEOFDAY
90 # include <sys/time.h>
91 # else
92 # include <ctime>
93 # endif
94 
95 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
96 /*!
97 \file elapsed_seconds.hpp
98 \brief Function that returns the elapsed seconds from first call.
99 */
100 
101 /*!
102 Returns the elapsed number since the first call to this function.
103 
104 This routine tries is accurate to within .02 seconds.
105 It does not necessary work for time intervals that are less than a day.
106 \li
107 If running under the Microsoft system, it uses \c ::%GetSystemTime for timing.
108 \li
109 Otherwise, if \c gettimeofday is available, it is used.
110 \li
111 Otherwise, \c std::clock() is used.
112 
113 \return
114 The number of seconds since the first call to \c elapsed_seconds.
115 */
116 inline double elapsed_seconds(void)
117 // --------------------------------------------------------------------------
118 # if CPPAD_USE_CPLUSPLUS_2011
120  static bool first_ = true;
121  static std::chrono::time_point<std::chrono::steady_clock> start_;
122  if( first_ )
123  { start_ = std::chrono::steady_clock::now();
124  first_ = false;
125  return 0.0;
126  }
127  std::chrono::time_point<std::chrono::steady_clock> now;
128  now = std::chrono::steady_clock::now();
129  std::chrono::duration<double> difference = now - start_;
130  return difference.count();
131 }
132 // --------------------------------------------------------------------------
133 # elif _MSC_VER
134 { return microsoft_timer(); }
135 // --------------------------------------------------------------------------
136 # elif CPPAD_HAS_GETTIMEOFDAY
138  static bool first_ = true;
139  static struct timeval tv_;
140  struct timeval tv;
141  if( first_ )
142  { gettimeofday(&tv_, CPPAD_NULL);
143  first_ = false;
144  return 0.;
145  }
146  gettimeofday(&tv, CPPAD_NULL);
147  assert( tv.tv_sec >= tv_.tv_sec );
148 
149  double sec = double(tv.tv_sec - tv_.tv_sec);
150  double usec = double(tv.tv_usec) - double(tv_.tv_usec);
151  double diff = sec + 1e-6*usec;
152 
153  return diff;
154 }
155 // --------------------------------------------------------------------------
156 # else // Not CPPAD_USE_CPLUSPLUS_2011 or CPPAD_HAS_GETTIMEOFDAY
158  static bool first_ = true;
159  static double tic_;
160  double tic;
161  if( first_ )
162  { tic_ = double(std::clock());
163  first_ = false;
164  return 0.;
165  }
166  tic = double( std::clock() );
167 
168  double diff = (tic - tic_) / double(CLOCKS_PER_SEC);
169 
170  return diff;
171 }
172 # endif
173 // --------------------------------------------------------------------------
174 } // END_CPPAD_NAMESPACE
175 # endif
Define processor symbols and macros that are used by CppAD.
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
#define CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
Check that the first call to a routine is not during parallel execution mode.
File used to define the CppAD multi-threading allocator class.
double elapsed_seconds(void)
Returns the elapsed number since the first call to this function.