clockWC.cpp

Go to the documentation of this file.
00001 /**
00002  * @file clockWC.cpp
00003  *    Definitions for a simple class that implements an Ansi C wall clock timer
00004  *     (see \ref Cantera::clockWC).
00005  */
00006 /*
00007  * $Author: hkmoffa $
00008  * $Revision: 368 $
00009  * $Date: 2010-01-03 19:46:26 -0500 (Sun, 03 Jan 2010) $
00010  */
00011 /*
00012  * Copywrite 2004 Sandia Corporation. Under the terms of Contract
00013  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00014  * retains certain rights in this software.
00015  * See file License.txt for licensing information.
00016  */
00017 
00018 #include <time.h>
00019 #include "clockWC.h"
00020 namespace Cantera {
00021 
00022  
00023   clockWC::clockWC() :
00024     last_num_ticks(clock()),
00025     clock_rollovers(0u),
00026     start_ticks(0),
00027     inv_clocks_per_sec(1./(double)CLOCKS_PER_SEC),
00028     clock_width((double)(1L<<((int)sizeof(clock_t)*8-2))*4./(double)CLOCKS_PER_SEC)
00029   {
00030     start_ticks = last_num_ticks;
00031   }
00032 
00033   /*
00034    * Reinitialize the tick counters within the object
00035    */
00036   double clockWC::start()
00037   {
00038     start_ticks = last_num_ticks = clock();
00039     clock_rollovers = 0u;
00040     return 0.0;
00041   }
00042 
00043   /*
00044    *    Returns system cpu and wall clock time in seconds. This
00045    *    is a strictly Ansi C timer, since clock() is defined as an
00046    *    Ansi C function. On some machines clock() returns type
00047    *    unsigned long (HP) and on others (SUN) it returns type long.
00048    *       An attempt to recover the actual time for clocks which have
00049    *    rolled over is made also. However, it only works if this 
00050    *    function is called fairly regularily during
00051    *    the solution procedure.
00052    *
00053    *    clock() -> returns the time in microseconds. Division by
00054    *               the macro CLOCKS_PER_SEC recovers the time in seconds.
00055    */
00056   double clockWC::secondsWC()
00057   {
00058     clock_t num_ticks = clock();
00059     if (num_ticks < last_num_ticks) clock_rollovers++;
00060     double value = (num_ticks - start_ticks) * inv_clocks_per_sec;
00061     if (clock_rollovers) value += clock_rollovers * clock_width;
00062     last_num_ticks = num_ticks;
00063     return(value);
00064   }
00065 }
Generated by  doxygen 1.6.3