1 /* 2 * Created by Phil on 05/08/2013. 3 * Copyright 2013 Two Blue Cubes Ltd. All rights reserved. 4 * 5 * Distributed under the Boost Software License, Version 1.0. (See accompanying 6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 */ 8 9 #include "catch_timer.h" 10 #include "catch_platform.h" 11 12 #ifdef __clang__ 13 #pragma clang diagnostic push 14 #pragma clang diagnostic ignored "-Wc++11-long-long" 15 #endif 16 17 #ifdef CATCH_PLATFORM_WINDOWS 18 19 # include "catch_windows_h_proxy.h" 20 21 #else 22 23 #include <sys/time.h> 24 25 #endif 26 27 namespace Catch { 28 29 namespace { 30 #ifdef CATCH_PLATFORM_WINDOWS getCurrentTicks()31 UInt64 getCurrentTicks() { 32 static UInt64 hz=0, hzo=0; 33 if (!hz) { 34 QueryPerformanceFrequency( reinterpret_cast<LARGE_INTEGER*>( &hz ) ); 35 QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &hzo ) ); 36 } 37 UInt64 t; 38 QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &t ) ); 39 return ((t-hzo)*1000000)/hz; 40 } 41 #else 42 UInt64 getCurrentTicks() { 43 timeval t; 44 gettimeofday(&t,CATCH_NULL); 45 return static_cast<UInt64>( t.tv_sec ) * 1000000ull + static_cast<UInt64>( t.tv_usec ); 46 } 47 #endif 48 } 49 start()50 void Timer::start() { 51 m_ticks = getCurrentTicks(); 52 } getElapsedMicroseconds() const53 unsigned int Timer::getElapsedMicroseconds() const { 54 return static_cast<unsigned int>(getCurrentTicks() - m_ticks); 55 } getElapsedMilliseconds() const56 unsigned int Timer::getElapsedMilliseconds() const { 57 return static_cast<unsigned int>(getElapsedMicroseconds()/1000); 58 } getElapsedSeconds() const59 double Timer::getElapsedSeconds() const { 60 return getElapsedMicroseconds()/1000000.0; 61 } 62 63 } // namespace Catch 64 65 #ifdef __clang__ 66 #pragma clang diagnostic pop 67 #endif 68