Category Archives: My Code

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

A Literal Converter for Integers

Introduction

Electronic engineers often feel the need to plot experiment data observed from electronic instruments (e.g. Oscilloscope meters). More often than not, the data is stored in one format (e.g. hexadecimal), while the plotting software needs another input format (e.g. decimal). This is quite inconvenient. I finally decided to wrap the conversion into a C++ class: CLiteralConverter. In practice, integer literals usually come in four guises: decimal, octal, hexadecimal, and binary literals. So CLiteralConverter only provides conversion between these four formats.

 

Although demonstrated with a MFC demo application, The code is written in standard C++. So it can be used with different platforms easily.
 

 

C++ Constructor & Destructor

The following piece of code shows the birth and death of an object

under various circumstances: when the constructor and destructor are called.

 

#include <iostream>

using std::cout;

using std::endl;

 

class CScopeTest

{

private:

      static int count;

      int index;

public:

      CScopeTest() {index = count++; cout<<"ctor"<<index<<endl;}     

      ~CScopeTest() {cout<<"dtor"<<index<<endl;}           

     

};

int CScopeTest::count = 1;

 

void f()

{

    CScopeTest t3;

}

 

CScopeTest t1;

 

int main()

{

    cout<<">> main()\n";

    CScopeTest t2;

    static CScopeTest t4;

    f();

    cout<<"<< main()\n";

    return 0;

}

 

Result:

ctor1
>> main()
ctor2
ctor3
ctor4
dtor4
<< main()
dtor2
dtor3
dtor1