Wall Clock Timer:
Added class that implements a simple wall clock timer
This commit is contained in:
parent
5698fc7752
commit
1226773520
4 changed files with 162 additions and 3 deletions
|
|
@ -1,3 +1,3 @@
|
|||
SET (CTBASE_SRCS misc.cpp ct2ctml.cpp ctml.cpp
|
||||
plots.cpp stringUtils.cpp xml.cpp)
|
||||
plots.cpp stringUtils.cpp xml.cpp clockWC.cpp)
|
||||
ADD_LIBRARY(ctbase ${CTBASE_SRCS})
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ PIC_FLAG=@PIC@
|
|||
|
||||
CXX_FLAGS = @CXXFLAGS@ $(LOCAL_DEFS) $(CXX_OPT) $(PIC_FLAG) $(DEBUG_FLAG)
|
||||
|
||||
BASE_OBJ = ct2ctml.o ctml.o misc.o plots.o stringUtils.o xml.o
|
||||
BASE_OBJ = ct2ctml.o ctml.o misc.o plots.o stringUtils.o xml.o clockWC.o
|
||||
|
||||
BASE_H = ct_defs.h ctexceptions.h logger.h XML_Writer.h \
|
||||
ctml.h plots.h stringUtils.h xml.h config.h utilities.h \
|
||||
Array.h vec_functions.h global.h FactoryBase.h
|
||||
Array.h vec_functions.h global.h FactoryBase.h clockWC.h
|
||||
|
||||
CXX_INCLUDES = -I. @CXX_INCLUDES@
|
||||
LIB = @buildlib@/libctbase.a
|
||||
|
|
|
|||
65
Cantera/src/base/clockWC.cpp
Normal file
65
Cantera/src/base/clockWC.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* @file clockWC.cpp
|
||||
* Definitions for a simple class that implements an Ansi C wall clock timer
|
||||
* (see \ref Cantera::clockWC).
|
||||
*/
|
||||
/*
|
||||
* $Author$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*/
|
||||
/*
|
||||
* Copywrite 2004 Sandia Corporation. Under the terms of Contract
|
||||
* DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
|
||||
* retains certain rights in this software.
|
||||
* See file License.txt for licensing information.
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include "clockWC.h"
|
||||
namespace Cantera {
|
||||
|
||||
|
||||
clockWC::clockWC() :
|
||||
last_num_ticks(clock()),
|
||||
clock_rollovers(0u),
|
||||
start_ticks(0),
|
||||
inv_clocks_per_sec(1./(double)CLOCKS_PER_SEC),
|
||||
clock_width((double)(1L<<((int)sizeof(clock_t)*8-2))*4./(double)CLOCKS_PER_SEC)
|
||||
{
|
||||
start_ticks = last_num_ticks;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reinitialize the tick counters within the object
|
||||
*/
|
||||
double clockWC::start()
|
||||
{
|
||||
start_ticks = last_num_ticks = clock();
|
||||
clock_rollovers = 0u;
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns system cpu and wall clock time in seconds. This
|
||||
* is a strictly Ansi C timer, since clock() is defined as an
|
||||
* Ansi C function. On some machines clock() returns type
|
||||
* unsigned long (HP) and on others (SUN) it returns type long.
|
||||
* An attempt to recover the actual time for clocks which have
|
||||
* rolled over is made also. However, it only works if this
|
||||
* function is called fairly regularily during
|
||||
* the solution procedure.
|
||||
*
|
||||
* clock() -> returns the time in microseconds. Division by
|
||||
* the macro CLOCKS_PER_SEC recovers the time in seconds.
|
||||
*/
|
||||
double clockWC::secondsWC()
|
||||
{
|
||||
clock_t num_ticks = clock();
|
||||
if (num_ticks < last_num_ticks) clock_rollovers++;
|
||||
double value = (num_ticks - start_ticks) * inv_clocks_per_sec;
|
||||
if (clock_rollovers) value += clock_rollovers * clock_width;
|
||||
last_num_ticks = num_ticks;
|
||||
return(value);
|
||||
}
|
||||
}
|
||||
94
Cantera/src/base/clockWC.h
Normal file
94
Cantera/src/base/clockWC.h
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/**
|
||||
* @file clockWC.h
|
||||
* Declarations for a simple class that implements an Ansi C wall clock timer
|
||||
* (see \ref Cantera::clockWC).
|
||||
*/
|
||||
/*
|
||||
* $Author$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*/
|
||||
/*
|
||||
* Copywrite 2004 Sandia Corporation. Under the terms of Contract
|
||||
* DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
|
||||
* retains certain rights in this software.
|
||||
* See file License.txt for licensing information.
|
||||
*/
|
||||
|
||||
#ifndef CT_CLOCKWC_H
|
||||
#define CT_CLOCKWC_H
|
||||
|
||||
#include <time.h>
|
||||
namespace Cantera {
|
||||
|
||||
//! The class provides the wall clock timer in seconds
|
||||
/*!
|
||||
* This routine relies on the ANSI C routine, clock(), for
|
||||
* its basic operation. Therefore, it should be fairly
|
||||
* portable.
|
||||
*
|
||||
* The clock will rollover if the calculation is long enough.
|
||||
* The wraparound time is roughly 72 minutes for a 32 bit system.
|
||||
* This object senses that by seeing if the raw tick counter is
|
||||
* has decreased from the last time. If it senses a wraparound has
|
||||
* occurred, it increments an internal counter to account for this.
|
||||
* Therefore, for long calculations, this object must be called
|
||||
* at regular intervals for the seconds timer to be accurate.
|
||||
*
|
||||
* An example of how to use the timer is given below. timeToDoCalcs
|
||||
* countains the wall clock time calculated for the operation.
|
||||
*
|
||||
*
|
||||
* @code
|
||||
* clockWC wc;
|
||||
* do_hefty_calculations_atLeastgreaterThanAMillisecond();
|
||||
* double timeToDoCalcs = wc.secondsWC();
|
||||
* @endcode
|
||||
*
|
||||
* In general, the process to be timed must take more than a millisecond
|
||||
* for this clock to enough of a significant resolution to be
|
||||
* accurate.
|
||||
*
|
||||
* @ingroup globalUtilFuncs
|
||||
*
|
||||
*/
|
||||
class clockWC {
|
||||
public:
|
||||
//! Constructor
|
||||
/*!
|
||||
* This also serves to initialize the ticks within the object
|
||||
*/
|
||||
clockWC();
|
||||
|
||||
//! Resets the internal counters and returns the wall clock time
|
||||
//! in seconds
|
||||
double start();
|
||||
|
||||
//! Returns the wall clock time in seconds since the last reset.
|
||||
double secondsWC();
|
||||
|
||||
private:
|
||||
//! Counters the value of the number of ticks from the last call.
|
||||
clock_t last_num_ticks;
|
||||
|
||||
//! Number of clock rollovers since the last initialization
|
||||
/*!
|
||||
* The clock will rollover if the calculation is long enough.
|
||||
* This object senses that by seeing if the raw tick counter is
|
||||
* has decreased from the last time.
|
||||
*/
|
||||
unsigned int clock_rollovers;
|
||||
|
||||
//! Counter countaining the value of the number of ticks from
|
||||
//! the first call (or the reset call).
|
||||
clock_t start_ticks;
|
||||
|
||||
//! internal constant containing clock ticks per second
|
||||
const double inv_clocks_per_sec;
|
||||
|
||||
//! internal constant containing the total number of ticks
|
||||
//! per rollover.
|
||||
const double clock_width;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Reference in a new issue