00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _STOPWATCH_HXX
00039 #define _STOPWATCH_HXX
00040
00041 #ifndef __cplusplus
00042 # error This library requires C++
00043 #endif
00044
00045 #ifdef HAVE_CONFIG_H
00046 # include <simgear_config.h>
00047 #endif
00048
00049 #if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
00050 # define HAVE_GETRUSAGE
00051 #endif
00052
00053 #if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
00054 # undef HAVE_GETRUSAGE
00055 #endif // WIN32
00056
00057 #if defined( HAVE_GETRUSAGE )
00058 # if defined( __FreeBSD__ )
00059 # include <sys/types.h>
00060 # endif
00061 # include <sys/time.h>
00062 # include <sys/resource.h>
00063 # include <unistd.h>
00064 #elif defined( WIN32 )
00065 # include <windows.h>
00066 #else
00067 # include <time.h>
00068 #endif
00069
00073 class StopWatch {
00074
00075 public:
00077 StopWatch() {
00078
00079 }
00080
00082 void start() {
00083
00084 t1_ = systemTime();
00085 }
00086
00088 void stop() {
00089 t2_ = systemTime();
00090
00091
00092 }
00093
00095 double elapsedSeconds()
00096 {
00097
00098 return t2_ - t1_;
00099 }
00100
00101 private:
00102 StopWatch(StopWatch&) { }
00103 void operator=(StopWatch&) { }
00104
00105 double systemTime()
00106 {
00107 #if defined( HAVE_GETRUSAGE )
00108 getrusage(RUSAGE_SELF, &resourceUsage_);
00109 double seconds = resourceUsage_.ru_utime.tv_sec
00110 + resourceUsage_.ru_stime.tv_sec;
00111 double micros = resourceUsage_.ru_utime.tv_usec
00112 + resourceUsage_.ru_stime.tv_usec;
00113 return seconds + micros/1.0e6;
00114 #elif defined( WIN32 )
00115 return double(GetTickCount()) * double(1e-3);
00116 #else
00117 return clock() / (double) CLOCKS_PER_SEC;
00118 #endif
00119 }
00120
00121
00122
00123 #if defined( HAVE_GETRUSAGE )
00124 struct rusage resourceUsage_;
00125 #endif
00126
00127 double t1_, t2_;
00128 };
00129
00130 #endif // _STOPWATCH_HXX
00131