00001 /************************************************************************** 00002 * Version: Simplified BSD License 00003 * URL: http://www.opensource.org/licenses/bsd-license.php 00004 * 00005 * Copyright (c) 2010, Joseph LaFata 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the following disclaimer. 00013 * - Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * - Neither the name of cppbench nor the names of its contributors 00017 * may be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 ***************************************************************************/ 00032 00033 #ifndef _STOPWATCH_H 00034 #define _STOPWATCH_H 00035 #pragma once 00036 00037 #include "DllExport.h" 00038 00039 #ifdef WIN32 00040 # define WIN_TIMER 00041 # include <windows.h> 00042 #else 00043 # include <unistd.h> 00044 # include <sys/resource.h> 00045 # include <sys/time.h> 00046 # define POSIX_TIMER 00047 # if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) 00048 # define MONOTONIC_TIMER 00049 # include <time.h> 00050 # endif 00051 #endif 00052 00053 #if defined(__GNUC__) || (_MSC_VER >= 1600) 00054 # include <stdint.h> 00055 #else 00056 typedef unsigned long long uint64_t; 00057 #endif 00058 00063 class CPPBENCH_API Stopwatch 00064 { 00065 public: 00066 00071 struct Time 00072 { 00073 double wall; 00074 double user; 00075 double sys; 00076 }; 00077 00081 Stopwatch(); 00082 00086 void start(); 00087 00091 void stop(); 00092 00096 void reset(); 00097 00103 Time getTime(); 00104 00105 private: 00106 00107 #ifdef WIN32 00108 ULONGLONG difftime(FILETIME* start, FILETIME* end); 00109 #endif 00110 00111 #if defined(WIN_TIMER) 00112 HANDLE mHandle; 00113 FILETIME mUserMark; 00114 FILETIME mSysMark; 00115 LARGE_INTEGER mWallMark; 00116 #elif defined(POSIX_TIMER) 00117 # if defined(MONOTONIC_TIMER) 00118 timespec mWallMark; 00119 # else 00120 timeval mWallMark; 00121 # endif 00122 timeval mUserMark; 00123 timeval mSysMark; 00124 #endif 00125 00127 uint64_t mWallElapsed; 00128 uint64_t mUserElapsed; 00129 uint64_t mSysElapsed; 00130 00131 bool mRunning; 00132 }; 00133 00134 #endif /* _STOPWATCH_H */