Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
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 )
00032         {
00033             mpi_initialized = 1;
00034         }
00035 #endif
00036         tAtBirth = runtime();
00037         tAtLast  = tAtBirth;
00038     }
00039     double time_since_birth()
00040     {
00041         return ( tAtLast = runtime() ) - tAtBirth;
00042     };
00043     double time_elapsed()
00044     {
00045         double tmp = tAtLast;
00046         return ( tAtLast = runtime() ) - tmp;
00047     }
00048 };
00049 
00050 inline double CpuTimer::runtime()
00051 {
00052 #ifdef MOAB_HAVE_MPI
00053     if( mpi_initialized )
00054         return MPI_Wtime();
00055     else
00056         return (double)clock() / CLOCKS_PER_SEC;
00057 #else
00058     return (double)clock() / CLOCKS_PER_SEC;
00059 #endif
00060 }
00061 }  // namespace moab
00062 
00063 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines