File indexing completed on 2025-01-18 10:13:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef __TBB_tick_count_H
0018 #define __TBB_tick_count_H
0019
0020 #include "tbb_stddef.h"
0021
0022 #if _WIN32||_WIN64
0023 #include "machine/windows_api.h"
0024 #elif __linux__
0025 #include <ctime>
0026 #else
0027 #include <sys/time.h>
0028 #endif
0029
0030 namespace tbb {
0031
0032
0033
0034 class tick_count {
0035 public:
0036
0037 class interval_t {
0038 long long value;
0039 explicit interval_t( long long value_ ) : value(value_) {}
0040 public:
0041
0042 interval_t() : value(0) {};
0043
0044
0045 explicit interval_t( double sec );
0046
0047
0048 double seconds() const;
0049
0050 friend class tbb::tick_count;
0051
0052
0053 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
0054
0055
0056 friend interval_t operator+( const interval_t& i, const interval_t& j ) {
0057 return interval_t(i.value+j.value);
0058 }
0059
0060
0061 friend interval_t operator-( const interval_t& i, const interval_t& j ) {
0062 return interval_t(i.value-j.value);
0063 }
0064
0065
0066 interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
0067
0068
0069 interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
0070 private:
0071 static long long ticks_per_second(){
0072 #if _WIN32||_WIN64
0073 LARGE_INTEGER qpfreq;
0074 int rval = QueryPerformanceFrequency(&qpfreq);
0075 __TBB_ASSERT_EX(rval, "QueryPerformanceFrequency returned zero");
0076 return static_cast<long long>(qpfreq.QuadPart);
0077 #elif __linux__
0078 return static_cast<long long>(1E9);
0079 #else
0080 return static_cast<long long>(1E6);
0081 #endif
0082 }
0083 };
0084
0085
0086 tick_count() : my_count(0) {};
0087
0088
0089 static tick_count now();
0090
0091
0092 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
0093
0094
0095 static double resolution() { return 1.0 / interval_t::ticks_per_second(); }
0096
0097 private:
0098 long long my_count;
0099 };
0100
0101 inline tick_count tick_count::now() {
0102 tick_count result;
0103 #if _WIN32||_WIN64
0104 LARGE_INTEGER qpcnt;
0105 int rval = QueryPerformanceCounter(&qpcnt);
0106 __TBB_ASSERT_EX(rval, "QueryPerformanceCounter failed");
0107 result.my_count = qpcnt.QuadPart;
0108 #elif __linux__
0109 struct timespec ts;
0110 int status = clock_gettime( CLOCK_REALTIME, &ts );
0111 __TBB_ASSERT_EX( status==0, "CLOCK_REALTIME not supported" );
0112 result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
0113 #else
0114 struct timeval tv;
0115 int status = gettimeofday(&tv, NULL);
0116 __TBB_ASSERT_EX( status==0, "gettimeofday failed" );
0117 result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
0118 #endif
0119 return result;
0120 }
0121
0122 inline tick_count::interval_t::interval_t( double sec ) {
0123 value = static_cast<long long>(sec*interval_t::ticks_per_second());
0124 }
0125
0126 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
0127 return tick_count::interval_t( t1.my_count-t0.my_count );
0128 }
0129
0130 inline double tick_count::interval_t::seconds() const {
0131 return value*tick_count::resolution();
0132 }
0133
0134 }
0135
0136 #endif