A class wrapping all kinds of timing under windows

This is a piece of old code written 2 years ago.
 
It invokes system function to implement pretty precision time measuring.
/********************************************************
 * Name:        CtTimer.h                               *
 * Copyright:   all rights reserved.                    *
 * Author:      tomgee,                                 *
 * Date:        21-09-03 01:34                          *
 * Description: a class wrapping all kinds of timing    *
 *              under windows.                          *
 ********************************************************
 * link KERNEl32.lib for QueryPerformanceCounter()      *
 * link winmm.lib for timeGetTime()                     *
 * Version:     1.0 2003-10-06                          *
 ********************************************************/


#ifndef CTTIMER_H
#define CTTIMER_H

#include <ctime>          // for clock()
#include <Windows.h>      // for timeGetTime() and QueryPerformanceCounter()

class CtTimer
{
public:
    CtTimer();
    ~CtTimer(){};
    
    enum{
        FN_CLOCK = 0,       // use function clock() .
        FN_TIMEGETTIME,     // use function timeGetTime().
        FN_QP,              // use function QueryPerformance();
        
        FN_MAX
        }; 
private:
    LARGE_INTEGER m_qstart, m_qend;
    LARGE_INTEGER m_perffreq;
    long m_start, m_end;
    short m_fn;
public:
    void SetFunction(short fn);
    void Start();
    void End();
    double Elapse();
    double Test();
        
};

CtTimer::CtTimer()
{
    QueryPerformanceFrequency(&m_perffreq);
    m_fn = FN_CLOCK;
}

void CtTimer::SetFunction(short fn)
{
    if (fn<FN_MAX) m_fn = fn;
}

void CtTimer::Start()
{   
    switch(m_fn)
    {
        case FN_CLOCK:        m_start =clock(); break;
        case FN_TIMEGETTIME:   m_start = timeGetTime(); break;
        case FN_QP:           QueryPerformanceCounter(&m_qstart);break;
        default:              ;  
    }

}

void CtTimer::End()
{
    switch(m_fn)
    {
        case FN_CLOCK:        m_end =clock(); break;
        case FN_TIMEGETTIME:   m_end = timeGetTime(); break;
        case FN_QP:           QueryPerformanceCounter(&m_qend);break;
        default:              ;  
    }
}

double CtTimer::Elapse()
{
    if (FN_QP == m_fn)
        return(((double)(m_qend.QuadPart- m_qstart.QuadPart)/(double)m_perffreq.QuadPart));
    else
        return (double)(m_end - m_start);    
}

double CtTimer::Test()
{
    Start();
    Sleep(500);
    End();
    return Elapse();
}

#endif


Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: