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