1 //======================================================================== 2 // 3 // GooTimer.cc 4 // 5 // This file is licensed under GPLv2 or later 6 // 7 // Copyright 2005 Jonathan Blandford <jrb@redhat.com> 8 // Copyright 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com> 9 // Copyright 2010 Hib Eris <hib@hiberis.nl> 10 // Inspired by gtimer.c in glib, which is Copyright 2000 by the GLib Team 11 // 12 //======================================================================== 13 14 #include <config.h> 15 16 #ifdef USE_GCC_PRAGMAS 17 #pragma implementation 18 #endif 19 20 #include "GooTimer.h" 21 #include <string.h> 22 23 #define USEC_PER_SEC 1000000 24 25 //------------------------------------------------------------------------ 26 // GooTimer 27 //------------------------------------------------------------------------ 28 GooTimer()29GooTimer::GooTimer() { 30 start(); 31 } 32 start()33void GooTimer::start() { 34 #ifdef HAVE_GETTIMEOFDAY 35 gettimeofday(&start_time, NULL); 36 #elif defined(_WIN32) 37 QueryPerformanceCounter(&start_time); 38 #endif 39 active = true; 40 } 41 stop()42void GooTimer::stop() { 43 #ifdef HAVE_GETTIMEOFDAY 44 gettimeofday(&end_time, NULL); 45 #elif defined(_WIN32) 46 QueryPerformanceCounter(&end_time); 47 #endif 48 active = false; 49 } 50 51 #ifdef HAVE_GETTIMEOFDAY getElapsed()52double GooTimer::getElapsed() 53 { 54 double total; 55 struct timeval elapsed; 56 57 if (active) 58 gettimeofday(&end_time, NULL); 59 60 if (start_time.tv_usec > end_time.tv_usec) { 61 end_time.tv_usec += USEC_PER_SEC; 62 end_time.tv_sec--; 63 } 64 65 elapsed.tv_usec = end_time.tv_usec - start_time.tv_usec; 66 elapsed.tv_sec = end_time.tv_sec - start_time.tv_sec; 67 68 total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6); 69 if (total < 0) 70 total = 0; 71 72 return total; 73 } 74 #elif defined(_WIN32) getElapsed()75double GooTimer::getElapsed() 76 { 77 LARGE_INTEGER freq; 78 double time_in_secs; 79 QueryPerformanceFrequency(&freq); 80 81 if (active) 82 QueryPerformanceCounter(&end_time); 83 84 time_in_secs = (double)(end_time.QuadPart-start_time.QuadPart)/(double)freq.QuadPart; 85 return time_in_secs * 1000.0; 86 87 } 88 #else getElapsed()89double GooTimer::getElapsed() 90 { 91 #warning "no support for GooTimer" 92 return 0; 93 } 94 #endif 95 96