#ifndef __PERFTIMER__
#define __PERFTIMER__

class PerfTimer
{
public:
    PerfTimer()
    {
        _time = 0;
        _start = 0;
        _min = _max = _count = _sum = 0;
        _first = true;
    }

    void Reset()
    {
        _time = 0;
        Start();
    }

    void Start()
    {
        QueryPerformanceCounter((LARGE_INTEGER *) &_start);
    }

    long Stop()
    {
        __int64 end;
        __int64 freq;
        QueryPerformanceCounter((LARGE_INTEGER*)&end);
        QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
        _time = (long)((end - _start) * 1000 / freq);
        return _time;
    }

    long GetMilliseconds()
    {
        return _time;
    }

    void Count(long ts)
    {
        _count++;
        _sum += ts;
        if (_first)
        {
            _min = _max = ts;
            _first = false;
        }
        if (ts < _min) _min = ts;
        if (ts > _max) _max = ts;
    }

    long Avg()
    {
        return (_sum / _count);
    }


private:
    long _time;

    bool _first;
    long _min;
    long _max;
    long _sum;
    long _count;
};


#endif // __PERFTIMER__

