tick_count.h

00001 /*
00002     Copyright 2005-2009 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
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 /* generic Unix */
00031 #include <sys/time.h>
00032 #endif /* (choice of OS) */
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 /* TBB_USE_ASSERT */
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 /* generic Unix */
00104     struct timeval tv;
00105 #if TBB_USE_ASSERT
00106     int status = 
00107 #endif /* TBB_USE_ASSERT */
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 /*(choice of OS) */
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 /* generic Unix */
00124     value = static_cast<long long>(sec*1E6);
00125 #endif /* (choice of OS) */
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 /* generic Unix */
00140     return value*1E-6;
00141 #endif /* (choice of OS) */
00142 }
00143 
00144 } // namespace tbb
00145 
00146 #endif /* __TBB_tick_count_H */
00147 

Copyright © 2005-2009 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.