Cantera  2.0
clockWC.cpp
Go to the documentation of this file.
1 /**
2  * @file clockWC.cpp
3  * Definitions for a simple class that implements an Ansi C wall clock timer
4  * (see \ref Cantera::clockWC).
5  */
6 /*
7  * Copyright 2004 Sandia Corporation. Under the terms of Contract
8  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
9  * retains certain rights in this software.
10  * See file License.txt for licensing information.
11  */
12 
13 #include <time.h>
14 #include "cantera/base/clockWC.h"
15 namespace Cantera
16 {
17 
18 
20  last_num_ticks(clock()),
21  clock_rollovers(0u),
22  start_ticks(0),
23  inv_clocks_per_sec(1./(double)CLOCKS_PER_SEC),
24  clock_width((double)(1L<<((int)sizeof(clock_t)*8-2))*4./(double)CLOCKS_PER_SEC)
25 {
27 }
28 
29 /*
30  * Reinitialize the tick counters within the object
31  */
33 {
34  start_ticks = last_num_ticks = clock();
35  clock_rollovers = 0u;
36  return 0.0;
37 }
38 
39 /*
40  * Returns system cpu and wall clock time in seconds. This
41  * is a strictly Ansi C timer, since clock() is defined as an
42  * Ansi C function. On some machines clock() returns type
43  * unsigned long (HP) and on others (SUN) it returns type long.
44  * An attempt to recover the actual time for clocks which have
45  * rolled over is made also. However, it only works if this
46  * function is called fairly regularily during
47  * the solution procedure.
48  *
49  * clock() -> returns the time in microseconds. Division by
50  * the macro CLOCKS_PER_SEC recovers the time in seconds.
51  */
53 {
54  clock_t num_ticks = clock();
55  if (num_ticks < last_num_ticks) {
57  }
58  double value = (num_ticks - start_ticks) * inv_clocks_per_sec;
59  if (clock_rollovers) {
60  value += clock_rollovers * clock_width;
61  }
62  last_num_ticks = num_ticks;
63  return(value);
64 }
65 }