AntPerfTimer.h 1.98 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
//  ---------------------------------------------------------------------------
//
//  @file       AntPerfTimer.h
//  @brief      A performance (precision) timer for benchs
//  @author     Philippe Decaudin
//  @license    This file is part of the AntTweakBar library.
//              For conditions of distribution and use, see License.txt
//
//  note:       No cpp file is needed, everything is defined in this header
//
//  ---------------------------------------------------------------------------

#if !defined ANT_PERF_TIMER_INCLUDED
#define ANT_PERF_TIMER_INCLUDED

#ifndef  __cplusplus
#   error This is a C++ header
#endif  // __cplusplus


#if defined(WIN32) || defined(WIN64) || defined(_WIN32) || defined(_WIN64)

    #include <windows.h>
    #include <tchar.h>

    struct PerfTimer
    {
        inline        PerfTimer()   { if( !QueryPerformanceFrequency(&Freq) ) MessageBox(NULL, _T("Precision timer not supported"), _T("Problem"), MB_ICONEXCLAMATION); Reset(); }
        inline void   Reset()       { QueryPerformanceCounter(&Start); }
        inline double GetTime()     { if( QueryPerformanceCounter(&End) ) return ((double)End.QuadPart - (double)Start.QuadPart)/((double)Freq.QuadPart); else return 0; }
    protected:
        LARGE_INTEGER Start, End, Freq;
    };

#else // !_WIN (-> LINUX)

    #include <sys/time.h>
    #include <unistd.h>

    struct PerfTimer
    {
        inline        PerfTimer()   { Reset(); }
        inline void   Reset()       { gettimeofday(&Start, &TZ); }
        inline double GetTime()     { gettimeofday(&End,&TZ); 
                                      double t1 = (double)Start.tv_sec + (double)Start.tv_usec/(1000*1000);
                                      double t2 = (double)End.tv_sec + (double)End.tv_usec/(1000*1000);
                                      return t2-t1; }
    protected:
        struct timeval Start, End;
        struct timezone TZ;
    };

#endif // _WIN


#endif // ANT_PERF_TIMER_INCLUDED