Calculating time by the C++ code

后端 未结 7 835
囚心锁ツ
囚心锁ツ 2021-01-02 22:21

I know this question has been asked few times over SO but none of them is really helping me out, so asking again.

I am using windows xp and running visual studio c++

7条回答
  •  温柔的废话
    2021-01-02 23:11

    Here is what I use to print time in milliseconds.

    void StartTimer( _int64 *pt1 )
    {
       QueryPerformanceCounter( (LARGE_INTEGER*)pt1 );
    }
    
    double StopTimer( _int64 t1 )
    {
       _int64 t2, ldFreq;
    
       QueryPerformanceCounter( (LARGE_INTEGER*)&t2 );
       QueryPerformanceFrequency( (LARGE_INTEGER*)&ldFreq );
       return ((double)( t2 - t1 ) / (double)ldFreq) * 1000.0;
    }
    

    Use it like this:

        _int64 t1;
    
        StartTimer( &t1 );
    
        // do some stuff
    
        printf( "Time = %.3f\n", StopTimer( t1 ));
    

提交回复
热议问题