00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __TBB_tick_count_H
00030 #define __TBB_tick_count_H
00031
00032 #include "tbb_stddef.h"
00033
00034 #if _WIN32||_WIN64
00035 #include "machine/windows_api.h"
00036 #elif __linux__
00037 #include <ctime>
00038 #else
00039 #include <sys/time.h>
00040 #endif
00041
00042 namespace tbb {
00043
00045
00046 class tick_count {
00047 public:
00049 class interval_t {
00050 long long value;
00051 explicit interval_t( long long value_ ) : value(value_) {}
00052 public:
00054 interval_t() : value(0) {};
00055
00057 explicit interval_t( double sec );
00058
00060 double seconds() const;
00061
00062 friend class tbb::tick_count;
00063
00065 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
00066
00068 friend interval_t operator+( const interval_t& i, const interval_t& j ) {
00069 return interval_t(i.value+j.value);
00070 }
00071
00073 friend interval_t operator-( const interval_t& i, const interval_t& j ) {
00074 return interval_t(i.value-j.value);
00075 }
00076
00078 interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
00079
00081 interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
00082 };
00083
00085 tick_count() : my_count(0) {};
00086
00088 static tick_count now();
00089
00091 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
00092
00093 private:
00094 long long my_count;
00095 };
00096
00097 inline tick_count tick_count::now() {
00098 tick_count result;
00099 #if _WIN32||_WIN64
00100 LARGE_INTEGER qpcnt;
00101 QueryPerformanceCounter(&qpcnt);
00102 result.my_count = qpcnt.QuadPart;
00103 #elif __linux__
00104 struct timespec ts;
00105 #if TBB_USE_ASSERT
00106 int status =
00107 #endif
00108 clock_gettime( CLOCK_REALTIME, &ts );
00109 __TBB_ASSERT( status==0, "CLOCK_REALTIME not supported" );
00110 result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
00111 #else
00112 struct timeval tv;
00113 #if TBB_USE_ASSERT
00114 int status =
00115 #endif
00116 gettimeofday(&tv, NULL);
00117 __TBB_ASSERT( status==0, "gettimeofday failed" );
00118 result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
00119 #endif
00120 return result;
00121 }
00122
00123 inline tick_count::interval_t::interval_t( double sec )
00124 {
00125 #if _WIN32||_WIN64
00126 LARGE_INTEGER qpfreq;
00127 QueryPerformanceFrequency(&qpfreq);
00128 value = static_cast<long long>(sec*qpfreq.QuadPart);
00129 #elif __linux__
00130 value = static_cast<long long>(sec*1E9);
00131 #else
00132 value = static_cast<long long>(sec*1E6);
00133 #endif
00134 }
00135
00136 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
00137 return tick_count::interval_t( t1.my_count-t0.my_count );
00138 }
00139
00140 inline double tick_count::interval_t::seconds() const {
00141 #if _WIN32||_WIN64
00142 LARGE_INTEGER qpfreq;
00143 QueryPerformanceFrequency(&qpfreq);
00144 return value/(double)qpfreq.QuadPart;
00145 #elif __linux__
00146 return value*1E-9;
00147 #else
00148 return value*1E-6;
00149 #endif
00150 }
00151
00152 }
00153
00154 #endif
00155