MOAB: Mesh Oriented datABase  (version 5.3.0)
CpuTimer.hpp
Go to the documentation of this file.
00001 #ifndef CPUTIMER_HPP
00002 #define CPUTIMER_HPP
00003 
00004 #include "moab/MOABConfig.h"
00005 #ifdef MOAB_HAVE_MPI
00006 #include "moab_mpi.h"
00007 #endif
00008 
00009 #include <ctime>
00010 
00011 namespace moab
00012 {
00013 
00014 class CpuTimer
00015 {
00016   private:
00017 #ifdef MOAB_HAVE_MPI
00018     int mpi_initialized;
00019 #endif
00020     double tAtBirth, tAtLast;
00021     double runtime();
00022 
00023   public:
00024     CpuTimer()
00025 #ifdef MOAB_HAVE_MPI
00026         : mpi_initialized( 0 )
00027 #endif
00028     {
00029 #ifdef MOAB_HAVE_MPI
00030         int flag = 0;
00031         if( MPI_SUCCESS == MPI_Initialized( &flag ) && flag ) { mpi_initialized = 1; }
00032 #endif
00033         tAtBirth = runtime();
00034         tAtLast  = tAtBirth;
00035     }
00036     double time_since_birth()
00037     {
00038         return ( tAtLast = runtime() ) - tAtBirth;
00039     };
00040     double time_elapsed()
00041     {
00042         double tmp = tAtLast;
00043         return ( tAtLast = runtime() ) - tmp;
00044     }
00045 };
00046 
00047 inline double CpuTimer::runtime()
00048 {
00049 #ifdef MOAB_HAVE_MPI
00050     if( mpi_initialized )
00051         return MPI_Wtime();
00052     else
00053         return (double)clock() / CLOCKS_PER_SEC;
00054 #else
00055     return (double)clock() / CLOCKS_PER_SEC;
00056 #endif
00057 }
00058 }  // namespace moab
00059 
00060 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines