Calculating time by the C++ code

后端 未结 7 782
囚心锁ツ
囚心锁ツ 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 22:54

    Here is a portable, self-contained, short implementation of exactly what you need: http://cplusplus.co.il/2009/08/19/portable-measurement-of-execution-time/

    If the resolution isnt fine enough, you should execute your test a couple of times.

    0 讨论(0)
  • 2021-01-02 23:01

    Usually people do something like this to measure some small time interval:

    t0 = getTime();
    
    for(int i = 0; i<1000000; ++i) {
      your code
    }
    
    t1 = getTime();
    
    timePassed = (t1-t0)/1000000;
    
    0 讨论(0)
  • 2021-01-02 23:02

    If you're on Windows, the GetTickCount() function is a handy way to get a timer with more resolution than 1 second. The resolution of GetTickCount depends on your operating system, but it's probably somewhere between 10 ms and 16 ms.

    Note that for quick operations, doing your code once won't be enough. Your code might run in like 0.02 ms, which means you'll get 0 from a counter such as GetTickCount. Your code may not execute for long enough for the timer to "tick" over to the next value. The solution is to run your code in a loop a million times or whatever, time the whole lot, then divide by a million.

    0 讨论(0)
  • 2021-01-02 23:04

    Kornel's suggestion of using QueryPerformanceCounter is an excellent one.

    If that doesn't work for you, and you don't mind another Windows-only solution, use the Windows "multimedia timers". Search the Visual Studio help files for "timeBeginPeriod", "timeEndPeriod", and "Using Multimedia Timers".

    To use the multimedia timers, you need to include the header and link with winmm.lib:

    #include <mmsystem.h>
    #pragma comment( lib, "winmm" )     //  need this library for multimedia timers
    
    0 讨论(0)
  • 2021-01-02 23:05

    You need a high precision timer. In case of Visual Studio use QueryPerformanceCounter.

    If the precision is still not enough, use compiler intristics:

    #include <intrin.h>
    #pragma intrinsic(__rdtsc)
    
    unsigned __int64 ticks = __rdtsc();
    

    See info on that intristic here.

    Both solutions are Windows only, the latter is probably MSVC only. I can post a similar solution for GCC/Linux if needed.

    0 讨论(0)
  • 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 ));
    
    0 讨论(0)
提交回复
热议问题