Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

tick_count.h

00001 /*
00002     Copyright 2005-2008 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 #include <stdio.h>
00031 #else /* generic Unix */
00032 #include <sys/time.h>
00033 #endif /* (choice of OS) */
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 /* TBB_DO_ASSERT */
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 /* generic Unix */
00105     struct timeval tv;
00106 #if TBB_DO_ASSERT
00107     int status = 
00108 #endif /* TBB_DO_ASSERT */
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 /*(choice of OS) */
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 /* generic Unix */
00125     value = static_cast<long long>(sec*1E6);
00126 #endif /* (choice of OS) */
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 /* generic Unix */
00141     return value*1E-6;
00142 #endif /* (choice of OS) */
00143 }
00144 
00145 } // namespace tbb
00146 
00147 #endif /* __TBB_tick_count_H */
00148 

Copyright © 2005-2008 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.