| CSocket project page | CSocket home page |
00001 /*----------------------------------------------------------------------------*/ 00002 /* */ 00003 /* StatTimer.h: interface for the CStatTimer class. */ 00004 /* */ 00005 /* Author: Mark Carrier (mark@carrierlabs.com) */ 00006 /* */ 00007 /*----------------------------------------------------------------------------*/ 00008 /* Copyright (c) 2006 CarrierLabs, LLC. All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions 00012 * are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 00017 * 2. Redistributions in binary form must reproduce the above copyright 00018 * notice, this list of conditions and the following disclaimer in 00019 * the documentation and/or other materials provided with the 00020 * distribution. 00021 * 00022 * 3. The name of the author may not be used to endorse or promote products 00023 * derived from this software without specific prior written permission. 00024 * 00025 * 4. The name "CarrierLabs" must not be used to 00026 * endorse or promote products derived from this software without 00027 * prior written permission. For written permission, please contact 00028 * mark@carrierlabs.com. 00029 * 00030 * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY 00031 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00032 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00033 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR 00034 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00035 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00036 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00037 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00038 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00039 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00040 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00041 * OF THE POSSIBILITY OF SUCH DAMAGE. 00042 *----------------------------------------------------------------------------*/ 00043 #ifndef __CSTATTIMER_H__ 00044 #define __CSTATTIMER_H__ 00045 00046 #include <string.h> 00047 00048 #if WIN32 00049 #include <Winsock2.h> 00050 #include <time.h> 00051 #endif 00052 00053 #ifdef _LINUX 00054 #include <stdio.h> 00055 #include <sys/time.h> 00056 #endif 00057 00058 #include "Host.h" 00059 00060 #if defined(WIN32) 00061 #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x) 00062 #else 00063 #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL) 00064 #endif 00065 00066 #define MILLISECONDS_CONVERSION 1000 00067 #define MICROSECONDS_CONVERSION 1000000 00068 00071 class CStatTimer { 00072 public: 00073 CStatTimer() 00074 { 00075 }; 00076 00077 ~CStatTimer() 00078 { 00079 }; 00080 00081 void Initialize() 00082 { 00083 memset(&m_startTime, 0, sizeof(struct timeval)); 00084 memset(&m_endTime, 0, sizeof(struct timeval)); 00085 }; 00086 00087 struct timeval GetStartTime() { return m_startTime; }; 00088 void SetStartTime() { GET_CLOCK_COUNT(&m_startTime); }; 00089 00090 struct timeval GetEndTime() { return m_endTime; }; 00091 void SetEndTime() { GET_CLOCK_COUNT(&m_endTime); }; 00092 00093 uint32 GetMilliSeconds() { return (CalcTotalUSec() / MILLISECONDS_CONVERSION); }; 00094 uint32 GetMicroSeconds() { return (CalcTotalUSec()); }; 00095 uint32 GetSeconds() { return (CalcTotalUSec() / MICROSECONDS_CONVERSION); }; 00096 00097 uint32 GetCurrentTime() 00098 { 00099 struct timeval tmpTime; 00100 GET_CLOCK_COUNT(&tmpTime); 00101 return ((tmpTime.tv_sec * MICROSECONDS_CONVERSION) + tmpTime.tv_usec); 00102 }; 00103 00104 private: 00105 uint32 CalcTotalUSec() { return (((m_endTime.tv_sec - m_startTime.tv_sec) * MICROSECONDS_CONVERSION) + 00106 (m_endTime.tv_usec - m_startTime.tv_usec)); }; 00107 00108 00109 private: 00110 struct timeval m_startTime; 00111 struct timeval m_endTime; 00112 }; 00113 00114 #endif // __CSTATTIMER_H__
|
hosts this site. |
Send comments to: CarrierLabs Developers |
