Cross-platform tool to accurately calculate function execution time

Table of contents

Use gettimeofday on linux

Use QueryPerformanceCounter and QueryPerformanceFrequency on windows

specific code

use


Use gettimeofday on linux

gettimeofdayis a function that gets the current time (seconds and microseconds since January 1, 1970). It is often used in programs that require precise timing, such as when measuring program execution time or adjusting time differences in web applications.

In C, you can use functions by including <sys/time.h>header files . gettimeofdayThe standard prototype of this function is as follows:

int gettimeofday(struct timeval *tv, struct timezone *tz);

Among them, tvis a timevalpointer to the structure used to store the current time value. timezoneThe structure can be used to get time zone information, but is usually ignored on most systems, set to NULL.

gettimeofdayThe function returns the current time as timevala structure containing seconds and microseconds. The structure is defined as follows:

struct timeval {  
    time_t tv_sec;     /* seconds */  
    suseconds_t tv_usec; /* microseconds */  
};

By using the gettimeofday function, you can get the seconds and microseconds of the current time, and perform corresponding calculations and operations.

Use QueryPerformanceCounter and QueryPerformanceFrequency on windows

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

QueryPerformanceCounterand QueryPerformanceFrequencyare two functions used in Windows programming to get the value and frequency of a high performance counter.

QueryPerformanceCounterfunction to get the current high performance counter value. It takes a LARGE_INTEGERpointer of type as a parameter and stores the current value of the high performance counter in the memory location pointed to by the pointer.

QueryPerformanceFrequencyThe function is used to get the frequency of the high performance counter. It also accepts a LARGE_INTEGERtype pointer as a parameter and stores the frequency of the high performance counter in the memory location pointed to by this pointer.

These two functions are often used together to calculate time intervals or program execution times. First, QueryPerformanceFrequencyget the frequency of the high performance counter by calling a function. Then, in the code segment that needs to be timed, call QueryPerformanceCounterthe function to get the start time and end time, and calculate the time difference between them.

Since the value and frequency of high-performance counters are hardware-dependent, the use of these functions requires the support of computer hardware. These functions may not work correctly if the computer does not support high performance counters.

It should be noted that since the accuracy of a high-performance counter may be affected by many factors, including hardware and operating system, its accuracy may not reach the nanometer level. In some cases, there may be some errors. Therefore, although these functions can provide relatively accurate time calculations, they are not guaranteed to be absolutely accurate.

  • The introduction of LARGE_INTERGER
    LARGE_INTEGERis a structure in Windows programming, which is used to represent signed 64-bit integers.
    LARGE_INTEGERThe structure contains two member variables, LowPartand HighPart. Among them, LowPartrepresents the lower 32 bits of the integer, and HighPartrepresents the upper 32 bits of the integer. Through these two member variables, a 64-bit integer can be fully represented.
    In addition to being used to represent integers, LARGE_INTERGERstructures can also be used in conjunction with other functions, such as bit shift operations, comparison operations, etc.
    It should be noted that LARGE_INTERGERa type is a specific type in Windows programming and may have a different definition and usage in other programming environments.
    The specific structure is as follows:
#if defined (__WIDL__)
typedef struct _LARGE_INTEGER {
#else
  typedef union _LARGE_INTEGER {
    __C89_NAMELESS struct {
      DWORD LowPart;
      LONG HighPart;
    } DUMMYSTRUCTNAME;
    struct {
      DWORD LowPart;
      LONG HighPart;
    } u;
#endif
    LONGLONG QuadPart;
  } LARGE_INTEGER;

specific code

#ifdef _WIN32
#include <sys/time.h>
#else
#include <windows.h>
#endif

#include <iostream>

#ifndef _WIN32
class timestramp{
public:
    timestramp()
    {
         gettimeofday(&tpstart,NULL);
    }

    ~timestramp()
    {
         gettimeofday(&tpend,NULL);
         dwTime = 1000000*(tpend.tv_sec- tpstart.tv_sec)+(tpend.tv_usec- tpstart.tv_usec);
         std::cout << "used time:" <<  dwTime << " us" << std::endl;
    }
private:
     struct timeval tpstart,tpend;
     double timeuse;
     unsigned long dwTime;
};

#else

class timestramp{
private:
    LARGE_INTEGER m_litmp;
    LONGLONG QPart2;
    LONGLONG QPart1;
    double dfMinus, dfFreq, dfTim;
    static double m_dTimeCount;
public:
    timestramp(){
        QueryPerformanceFrequency(&m_litmp);
        dfFreq = (double)m_litmp.QuadPart;
        QueryPerformanceCounter(&m_litmp);
        QPart1 = m_litmp.QuadPart;
    }

    ~timestramp(){
        QueryPerformanceCounter(&m_litmp);
        QPart2 = m_litmp.QuadPart; 
        dfMinus = (double)(QPart2 - QPart1);
        dfTim = dfMinus / dfFreq * 1000*1000;

        //显示时间
        std::string msg4 = "time:", msg3, msg5 = " us";
        char strTime[20] = "";
        sprintf(strTime, "%.6lf", dfTim);
        msg3 = strTime;
        msg4 += msg3;
        msg4 += msg5;
        std::cout << msg4.c_str() << std::endl;
    }
};
#endif

use

int main()
{
    {
        timestramp ti;
        for(int i = 0; i < 1000000; i++){
            //do anything
        }
        return 0;
    }
}

Guess you like

Origin blog.csdn.net/CHNIM/article/details/132068595