当前位置:小鱼儿玄机二站 > 计算机网络 > 电子石英钟调控

电子石英钟调控

文章作者:计算机网络 上传时间:2019-09-24
#ifndef CPU_TIMER_LK
#define CPU_TIMER_LK

#include <Windows.h>
#include <ctime>

namespace util
{
    //高精度计时
    struct high_accurary_timer
    {
        high_accurary_timer()
        {   
            QueryPerformanceFrequency(&m_freq);
            QueryPerformanceCounter(&m_startCount);
        }

        void restart() 
        {    
            QueryPerformanceCounter(&m_startCount); 
        } 

        double elaspe()//将clock数转为 秒
        {
            QueryPerformanceCounter(&m_endCount);   
            return (double)
                (m_endCount.QuadPart - m_startCount.QuadPart) / m_freq.QuadPart;
        }

    private:
         LARGE_INTEGER m_startCount;//开启时的clock数
         LARGE_INTEGER m_endCount;//结束时的clock数
         LARGE_INTEGER m_freq;//频率结构
    };

    struct std_timer
    {
        std_timer()
        {
            m_Time = std::clock();
        }

        void restart()
        {
            m_Time = std::clock(); 
        }

        double elaspe()//将clock数转为 秒
        {
            return  double(std::clock() - m_Time) / CLOCKS_PER_SEC; 
        }
    private:
        std::clock_t m_Time; 
    };

    template<class Timer>
    inline
    void timer_wait(int millseconds)
    {
        Timer timer;
        double stopSeconds = double(millseconds)/1000;
        while( timer.elaspe() < stopSeconds);
    }

    template<class Timer>
    inline
    void timer_wait(Timer& timer , int millseconds)
    {
        timer.restart();
        double stopSeconds = double(millseconds)/1000;
        while( timer.elaspe() < stopSeconds);
    }

    inline
    void timer_wait(int millseconds)
    {
        high_accurary_timer timer;
        double stopSeconds = double(millseconds)/1000;
        while( timer.elaspe() < stopSeconds);
    }

}

#endif
//该片段来自于http://outofmemory.cn

本文由小鱼儿玄机二站发布于计算机网络,转载请注明出处:电子石英钟调控

关键词: