How to use QueryPerformanceCounter?

后端 未结 4 1793
难免孤独
难免孤独 2020-11-22 08:36

I recently decided that I needed to change from using milliseconds to microseconds for my Timer class, and after some research I\'ve decided that QueryPerformanceCounter is

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 09:00

    I use these defines:

    /** Use to init the clock */
    #define TIMER_INIT \
        LARGE_INTEGER frequency; \
        LARGE_INTEGER t1,t2; \
        double elapsedTime; \
        QueryPerformanceFrequency(&frequency);
    
    
    /** Use to start the performance timer */
    #define TIMER_START QueryPerformanceCounter(&t1);
    
    /** Use to stop the performance timer and output the result to the standard stream. Less verbose than \c TIMER_STOP_VERBOSE */
    #define TIMER_STOP \
        QueryPerformanceCounter(&t2); \
        elapsedTime=(float)(t2.QuadPart-t1.QuadPart)/frequency.QuadPart; \
        std::wcout<

    Usage (brackets to prevent redefines):

    TIMER_INIT
    
    {
       TIMER_START
       Sleep(1000);
       TIMER_STOP
    }
    
    {
       TIMER_START
       Sleep(1234);
       TIMER_STOP
    }
    

    Output from usage example:

    1.00003 sec
    1.23407 sec
    

提交回复
热议问题