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 #include <stdio.h>
00031 #else
00032 #include <sys/time.h>
00033 #endif
00034
00035 namespace tbb {
00036
00038
00039 class tick_count {
00040 public:
00042 class interval_t {
00043 long long value;
00044 explicit interval_t( long long value_ ) : value(value_) {}
00045 public:
00047 interval_t() : value(0) {};
00048
00050 explicit interval_t( double sec );
00051
00053 double seconds() const;
00054
00055 friend class tbb::tick_count;
00056
00058 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
00059
00061 friend interval_t operator+( const interval_t& i, const interval_t& j ) {
00062 return interval_t(i.value+j.value);
00063 }
00064
00066 friend interval_t operator-( const interval_t& i, const interval_t& j ) {
00067 return interval_t(i.value-j.value);
00068 }
00069
00071 interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
00072
00074 interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
00075 };
00076
00078 tick_count() : my_count(0) {};
00079
00081 static tick_count now();
00082
00084 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
00085
00086 private:
00087 long long my_count;
00088 };
00089
00090 inline tick_count tick_count::now() {
00091 tick_count result;
00092 #if _WIN32||_WIN64
00093 LARGE_INTEGER qpcnt;
00094 QueryPerformanceCounter(&qpcnt);
00095 result.my_count = qpcnt.QuadPart;
00096 #elif __linux__
00097 struct timespec ts;
00098 #if TBB_DO_ASSERT
00099 int status =
00100 #endif
00101 clock_gettime( CLOCK_REALTIME, &ts );
00102 __TBB_ASSERT( status==0, "CLOCK_REALTIME not supported" );
00103 result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
00104 #else
00105 struct timeval tv;
00106 #if TBB_DO_ASSERT
00107 int status =
00108 #endif
00109 gettimeofday(&tv, NULL);
00110 __TBB_ASSERT( status==0, "gettimeofday failed" );
00111 result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
00112 #endif
00113 return result;
00114 }
00115
00116 inline tick_count::interval_t::interval_t( double sec )
00117 {
00118 #if _WIN32||_WIN64
00119 LARGE_INTEGER qpfreq;
00120 QueryPerformanceFrequency(&qpfreq);
00121 value = static_cast<long long>(sec*qpfreq.QuadPart);
00122 #elif __linux__
00123 value = static_cast<long long>(sec*1E9);
00124 #else
00125 value = static_cast<long long>(sec*1E6);
00126 #endif
00127 }
00128
00129 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
00130 return tick_count::interval_t( t1.my_count-t0.my_count );
00131 }
00132
00133 inline double tick_count::interval_t::seconds() const {
00134 #if _WIN32||_WIN64
00135 LARGE_INTEGER qpfreq;
00136 QueryPerformanceFrequency(&qpfreq);
00137 return value/(double)qpfreq.QuadPart;
00138 #elif __linux__
00139 return value*1E-9;
00140 #else
00141 return value*1E-6;
00142 #endif
00143 }
00144
00145 }
00146
00147 #endif
00148